"私人"成员变量 [英] "Private" Member Variables

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

问题描述

大家好,


我在开发中型项目时仍在学习Python。从我以前使用C ++的经验来看,我已经忘记了

信息隐藏的概念。我无法理解我在Python代码中应遵循此政策的程度,所以我想我会问小组。


我从上一篇文章中读到,要获得成员私有状态的成员

变量,我应该在变量名前加两个下划线(__)。

这确实有效,但在另一篇文章中,有人问我这是否真的需要b $ b。鉴于该语言的本质排除了任何编译时类型依赖关系,我想知道是否有任何好处

使用前导下划线命名变量并提供访问器功能

像这样:

C级:

...

def值(个体经营):

返回self .__ value

...


直接依赖C ++中的成员变量引起的问题

(编译时类型依赖,如上所述)在Python中不存在。那么

为什么要提供一个存取器呢?为什么不直接阅读和写成员变量?
?这里有什么我想念的吗?


你有什么想法?我应该在我的代码中构建多少隐私?

我应该使用以__开头的变量吗?和访问者?或者说,这是什么?b $ b在Python代码中根本没有必要(或正常)?


谢谢,

Scott


-

从我的电子邮件地址中删除.nospam给我发邮件。

Hi, everyone,

I''m still learning Python as I develop a medium-sized project. From my
previous experience with C++, I''ve burnt into my mind the notion of
information hiding. I''m having trouble understanding to what extent I
should follow this policy in my Python code so I thought I''d ask the group.

I read from a previous post that to attain a private-like status of member
variables, I should prefix the variable name with two underscores ("__").
This does work, but in another post someone asked me if this was really
necessary. Given that the very nature of the language precludes any
compile-time type dependencies I''m wondering if there is any benefit to
naming variables with leading underscores and providing accessor functions
like this:
class C:
...
def value(self):
return self.__value
...

The problems that arise from directly relying on the member variable in C++
(compile-time type dependency, as I said above) don''t exist in Python. So
why provide an accessor at all? Why not just allow direct reading and
writing of the member variable? Is there something here I''m missing?

What are your thoughts? How much privacy should I build into my code?
Should I be using variables beginning with "__" and accessors? Or is that
simply not necessary (or normal) in Python code?

Thanks,
Scott

--
Remove .nospam from my e-mail address to mail me.

推荐答案

Scott Brady Drummonds写道:
Scott Brady Drummonds wrote:
你有什么想法?我应该在我的代码中构建多少隐私?
我应该使用以__开头的变量吗?和访问者?或者,这在Python代码中根本不是必需的(或正常的)?
What are your thoughts? How much privacy should I build into my code?
Should I be using variables beginning with "__" and accessors? Or is that
simply not necessary (or normal) in Python code?




这里的座右铭是我们都同意成年人。如果班级用户

需要访问变量,为什么不直接使用?


如果你以后注意到你需要一个访问者方法,因为变量的值是值b / b。必须预先计算,然后使用属性[1]。对于

实例:


类Rectangle(对象):

def __init __(self,b,h):

self.b,self.h = b,h

def get_area(个体经营):

返回self.b * self.h

area = property(get_area)


在这种情况下,该属性是只读的,但如果你需要

,你也可以放置一个setter它。


[1]你需要属性的新式类,所以一定要从对象或它的一个后代继承




-

Ciao,

Matteo



The motto here is "we are all consenting adults". If the class users
need to access a variable, then why not make it directly available?

If you later notice that you''d need an accessor method, since the value
of the "variable" has got to be precomputed, then use properties[1]. For
instance:

class Rectangle(object):
def __init__(self, b, h):
self.b, self.h = b, h
def get_area(self):
return self.b * self.h
area = property(get_area)

In this case the property is read-only, but you can also put a setter if
you need it.

[1] You need new-style classes for properties, so make sure you inherit
from object or one of its descendants.

--
Ciao,
Matteo


Scott Brady Drummonds写道:
Scott Brady Drummonds wrote:
大家好,

我在开发一个中型项目时仍在学习Python。从我以前使用C ++的经历来看,我已经忘记了隐藏信息的概念。我很难理解我在Python代码中应该遵循这个策略的程度,所以我想我会问小组。

我从之前的帖子中读到了获得成员
变量的私有状态,我应该在变量名前加两个下划线(__)。
这确实有效,但在另一个帖子中有人问我这是不是真的<必要的。鉴于语言的本质排除了任何编译时类型的依赖关系,我想知道用引号下划线命名变量并提供访问器函数是否有任何好处
这样:
C级:
...
def值(个体经营):
返回自我.__值


那些问题直接依赖于C ++中的成员变量(编译时类型依赖,如上所述)在Python中不存在。那么为什么要提供一个存取器呢?为什么不直接阅读和写成员变量?这里有什么我想念的吗?

你有什么想法?我应该在我的代码中构建多少隐私?
我应该使用以__开头的变量吗?和访问者?或者这只是在Python代码中没有必要(或正常)?

谢谢,
Scott
Hi, everyone,

I''m still learning Python as I develop a medium-sized project. From my
previous experience with C++, I''ve burnt into my mind the notion of
information hiding. I''m having trouble understanding to what extent I
should follow this policy in my Python code so I thought I''d ask the group.

I read from a previous post that to attain a private-like status of member
variables, I should prefix the variable name with two underscores ("__").
This does work, but in another post someone asked me if this was really
necessary. Given that the very nature of the language precludes any
compile-time type dependencies I''m wondering if there is any benefit to
naming variables with leading underscores and providing accessor functions
like this:
class C:
...
def value(self):
return self.__value
...

The problems that arise from directly relying on the member variable in C++
(compile-time type dependency, as I said above) don''t exist in Python. So
why provide an accessor at all? Why not just allow direct reading and
writing of the member variable? Is there something here I''m missing?

What are your thoughts? How much privacy should I build into my code?
Should I be using variables beginning with "__" and accessors? Or is that
simply not necessary (or normal) in Python code?

Thanks,
Scott




我如果该属性

可以在没有验证的情况下自由修改,那么直接暴露属性没有问题。使用C ++和其他语言进行

的主要问题是,改变计算值

将破坏现有代码。

在python中,如果某个属性突然需要在分配时验证

时间,则必须在获取时计算。时间,你可以把它变成一个属性。

赢得客户代码;看看差异。


然而,为了保持一致性,我会做所有获取/设置访问

不能直接进入属性。


Steve



I see no problem with exposing the attribute directly, if that attribute
may be freely modified without verification. The main problem with doing
this in C++ and other languages, is that changing to a calculated value
down the road would break existing code.

In python, if an attribute suddenly needs to be validated at assignment
time, of has to be calculated at "get" time, you can make it a property.
Client code won;t see the difference.

However, for the sake of consistency, I would make all get/set accessed
that cannot be direct into properties.

Steve


周五, 2004年5月28日09:56:43 -0700,

" Scott Brady Drummonds" < SC ********************** @ intel.com>写道:


[信息隐藏? ]
On Fri, 28 May 2004 09:56:43 -0700,
"Scott Brady Drummonds" <sc**********************@intel.com> wrote:

[ information hiding? ]
...为什么不直接允许直接读写会员
变量?这里有什么我想念的吗?


为什么不呢?不,没有什么是你错过的。 Python需要

a我们都是成年人哲学。收集琐碎的

getter / setter只是杂乱无章(而且,恕我直言,这意味着你的班级

只是封装数据而不是功能;显然,
$ b但是,有时候,这是正确的事情。


如果有什么值得隐藏,写一个包装函数,

记录它,并使用它。如果没有什么值得隐藏的话,那么

不要写一个包装函数,记录成员,只需

直接访问成员。


你和任何人都知道你的申请。当你需要提供一个更大功能的界面而不是仅仅让剩下的代码<时,你就会知道(或者从经验中学习b
) br />
直接指向对象的成员。


最糟糕的是,你可以将个人成员转换为属性,并且

运行任意代码访问这些属性,但明确的是

比隐含更好。

你有什么想法?我应该在我的代码中构建多少隐私?我应该使用以__开头的变量吗?和访问者?或者只是在Python代码中没有必要(或正常)?
... Why not just allow direct reading and writing of the member
variable? Is there something here I''m missing?
Why not indeed? No, there''s nothing you''re missing. Python takes
a "we''re all adults here" philosophy. Collections of trivial
getters/setters are just clutter (and, IMHO, mean that your class
is merely encapsulating data rather than functionality; obviously,
though, there are times when that''s the Right Thing).

If there''s something worth "hiding," write a wrapper function,
document it, and use it. If there''s nothing worth hiding, then
don''t write a wrapper function, document the members, and just
access the members directly.

You know your application as well as anyone. You''ll know (or
learn from experience) when it''s time to present an interface to
larger functionality rather than just let the rest of the code
directly at your objects'' members.

At worst, you can convert individual members to properties, and
run arbitrary code to access those properties, but explicit is
better than implicit.
What are your thoughts? How much privacy should I build into my
code? Should I be using variables beginning with "__" and
accessors? Or is that simply not necessary (or normal) in
Python code?




只需记录你的意图,并坚持你的文档。


问候,

希瑟


-

Heather Coppersmith

那是不对的;那甚至没错。 - Wolfgang Pauli



Just document your intentions, and stick to your documentation.

Regards,
Heather

--
Heather Coppersmith
That''s not right; that''s not even wrong. -- Wolfgang Pauli


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

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