私有变量/方法 [英] private variables/methods

查看:69
本文介绍了私有变量/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




据我所知,在python中没有任何私有(我的意思是无法访问

来自对象的外部)方法/字段。


为什么?


在java / c ++中我可以将方法设为私有,这种方式对外界来说是无法访问的/>
世界。我认为让图书馆更加健壮有很大帮助。


i知道有某种方法可以让方法/字段变得私密,

但是人们仍然可以覆盖它的价值。


这是什么原因?


我主要是对设计理由感兴趣。


谢谢,

gabor


-

McDunn说,那是你的生命。有人总是在等待一个从未回家的人b $ b。总是有人喜欢比这更好的东西

东西爱他们。过了一段时间,你想要摧毁那个东西是什么,所以它不会再伤害你了。

- R. Bradbury,The Fog Horn ;

解决方案

文章< ma ******************* **************@python.org> ,gabor写道:

据我所知,在python中没有任何私有(我的意思是从对象外部无法访问)方法/字段。

为什么?




没有冒犯,但这就像第4000次有人问这个问题

这里。您可以先尝试搜索Google网上论坛吗?


Dave


-

..:[dave benjamin( ramenboy) - : - www.ramenfest.com - : - www.3dex.com ]:

:drinklifeoutofthec ontainer:


On Thu,2003-10-09 at 10:12,Dave Benjamin写道:

在文章< ma ************ *********************@python.org> ,gabor写道:

据我所知,在python中没有任何私有(我的意思是从对象外部无法访问)方法/字段。

为什么?



没有冒犯,但这就像第4000次有人问这个问题
。您可以先尝试搜索Google网上论坛吗?




你是对的,对不起。


阅读后档案:


i认为我没有表达得太好。

我会再试一次:


我知道你可以用__声明变量私有。

但我不喜欢前缀表示法。我不喜欢mMember或

lpszCommandLine样式表示法,还有


变量样式表示法。


至少对我而言,似乎增加了一个私人。某个地方的关键字是

更优雅。但是没有那样的东西。


这就是为什么在我看来这种语言的设计者不会给你b
推荐你可以使用私有变量,但如果你愿意,你可以使用这个''hack''(我的意思是__符号)。

谢谢,

gabor


hi,

as far as i know in python there aren''t any private (i mean not accessible
from the outside of the object) methods/fields.

why?

in java/c++ i can make a method private, this way unaccessible for the outside
world. i think it helps a lot to make for example a library more robust.

i know that there is some kind of notation to make a method/field private,
but one can still overwrite it''s value.

what''s the reason for this?

i''l mostly interested in the design reasons.

thanks,
gabor

--
That''s life for you, said McDunn. Someone always waiting for someone
who never comes home. Always someone loving something more than that
thing loves them. And after awhile you want to destroy whatever
that thing is, so it can''t hurt you no more.
-- R. Bradbury, "The Fog Horn"

解决方案

In article <ma*********************************@python.org> , gabor wrote:

as far as i know in python there aren''t any private (i mean not accessible
from the outside of the object) methods/fields.

why?



No offense, but this is like the 4000th time someone has asked that question
here. Could you try searching Google groups first?

Dave

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :


On Thu, 2003-10-09 at 10:12, Dave Benjamin wrote:

In article <ma*********************************@python.org> , gabor wrote:

as far as i know in python there aren''t any private (i mean not accessible
from the outside of the object) methods/fields.

why?



No offense, but this is like the 4000th time someone has asked that question
here. Could you try searching Google groups first?



you''re right, i''m sorry.

after reading the archives:

i think i didn''t express myself too well.
i''ll try again:

i''m aware of the fact that you can declare a variable private with "__".
but i just don''t like prefix-notation. i don''t like the mMember or
lpszCommandLine style notation, and also the


variable style ones.

at least for me it seemed that adding a "private" keyword somewhere is
more elegant. but there isn''t anything like that.

that''s why it seems to me that the designers of the language don''t
recommend you to use private variables at all, but if you want, you can
use this ''hack'' ( i mean the "__" notation).
thanks,
gabor


这篇关于私有变量/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆