ClassName.attribute vs self .__ class __。attribute [英] ClassName.attribute vs self.__class__.attribute

查看:90
本文介绍了ClassName.attribute vs self .__ class __。attribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我读过某个地方,首选使用

self .__ class __。属性,通过ClassName.attribute访问类(又名

静态)属性。我已经完成了这个并且它接合工作,直到我使用这种技术将b $ b子类化为一个类并且从那里开始了事情

搞砸了。我终于把它追溯到了自我.__ class __。属性!什么

正在发生的是,每个子类都超过了他们级别的类

属性,并且父级的从未设置过,所以当我

以为我确实在父母中设置了一个类属性,这是设置的孩子的每个孩子,并且每个孩子都拥有它自己的实例!由于

它是一个锁定机制,调试很有趣......所以,我建议永远不要使用self .__ class __。属性,除非你不介意它是孩子

覆盖它,但如果你想要一个真正的顶级类属性,那么到处使用

ClassName.attribute!


我希望书籍和教程明确地提到这一点....


Gabriel

解决方案

Gabriel Rossetti写道:


大家好,


我读过某个地方,最好使用

self .__ class __。通过ClassName.attribute属性来访问类(又名

static)属性。我已经完成了这个并且它接合工作,直到我使用这种技术将b $ b子类化为一个类并且从那里开始了事情

搞砸了。我终于把它追溯到了自我.__ class __。属性!什么

正在发生的是,每个子类都超过了他们级别的类

属性,并且父级的从未设置过,所以当我

以为我确实在父母中设置了一个类属性,这是设置的孩子的每个孩子,并且每个孩子都拥有它自己的实例!由于

它是一个锁定机制,调试很有趣......所以,我建议永远不要使用self .__ class __。属性,除非你不介意它是孩子

覆盖它,但如果你想要一个真正的顶级类属性,那么到处使用

ClassName.attribute!


我希望书籍和教程明确提到这个....


Gabriel



如果你定义一个班级与类

属性同名的实例变量,Python如何能够区分这两个?这是一个功能

不是问题。 Getter查找实例属性,如果找不到它

查找类属性,向上查找。 Zope使用这种行为来做各种各样整洁的东西。


-Larry Bates


< blockquote> 5月5日17:40,Gabriel Rossetti< gabriel.rosse ... @ arimaz.com>

写道:


大家好,


我读过某个地方,首选使用

self .__ class __。属性,通过ClassName.attribute来访问类(又名

静态)属性。



它甚至更喜欢使用self.attribute,除非你知道你有一个实例和一个类属性的

相同的名字(但是如果

你需要知道你正在访问哪一个,那么你有一个严重的

设计问题)。


我已经完成了这个并且它接合工作,直到我使用这种技术将b $ b子类化为一个类并从那里开始工作

搞砸了。我终于把它追溯到了自我.__ class __。属性!什么

正在发生的是,每个孩子班级都超过了他们级别的班级

属性,



这就是为什么它更喜欢从类中访问属性(或者更简单地从将从类中获取它们的实例获得
) -

子类可能有极其充分的理由让它拥有它自己的属性

属性。


且父亲的's'从未设置过,



这是另一个问题。


所以当我是

我以为我确实在父母中设置了一个类属性,这是设置的孩子的原因,并且每个孩子都拥有它自己的实例!由于

它是一个锁定机制,调试很有趣...



我完全相信它,你确实拥有我的完全同情。


所以,我建议永远不要使用self .__ class __。属性来使用
,除非你不介意它孩子

覆盖它,但是如果你想要一个真正的顶级类属性,请到处使用

ClassName.attribute!



我不会那样表达。我自己的经验是,

大部分时间,你知道子类什么时候可以让他们自己的属性实例和/或
不是,所以在(极其罕见的)很少的情况下你使用__name_mangling是不行的。


现在我会说那个*在不知不觉中*覆盖你自己

基类的一个属性当你没想到它会发生时,主要是一个迹象表明

那里有你不喜欢的东西't(没有?)在Python中完全得到wrt / lookup /

赋值/名称空间等规则。


我希望书籍和教程明确地提到了这个....



可能。 OTHO,如果您了解Python的查找(名称解析)

规则,MyBaseClass.attrib和

self .__ class __。attrib之间的区别应该是显而易见的。但这肯定可以是教程中的一个好例子! - )


" br ********** *******@gmail.com" < br ***************** @ gmail.com>

写道:


5月5日17:40,Gabriel Rossetti< gabriel.rosse ... @ arimaz.com>

写道:


>大家好,

我曾在某处读过,最好使用ClassName.attribute上的self .__ class __。属性来访问类(又名
静态)属性。



它甚至更喜欢使用self.attribute,



我准确写完了那个,但我想到他可能会想要*从他的方法中将*赋予自我.__ class __。属性(或

HisClass.attribute)* case self.attribute

会有很大不同。


Hello everyone,

I had read somewhere that it is preferred to use
self.__class__.attribute over ClassName.attribute to access class (aka
static) attributes. I had done this and it seamed to work, until I
subclassed a class using this technique and from there on things started
screwing up. I finally tracked it down to self.__class__.attribute! What
was happening is that the child classes each over-rode the class
attribute at their level, and the parent''s was never set, so while I was
thinking that I had indeed a class attribute set in the parent, it was
the child''s that was set, and every child had it''s own instance! Since
it was a locking mechanism, lots of fun to debug... So, I suggest never
using self.__class__.attribute, unless you don''t mind it''s children
overriding it, but if you want a truly top-level class attribute, use
ClassName.attribute everywhere!

I wish books and tutorials mentioned this explicitly....

Gabriel

解决方案

Gabriel Rossetti wrote:

Hello everyone,

I had read somewhere that it is preferred to use
self.__class__.attribute over ClassName.attribute to access class (aka
static) attributes. I had done this and it seamed to work, until I
subclassed a class using this technique and from there on things started
screwing up. I finally tracked it down to self.__class__.attribute! What
was happening is that the child classes each over-rode the class
attribute at their level, and the parent''s was never set, so while I was
thinking that I had indeed a class attribute set in the parent, it was
the child''s that was set, and every child had it''s own instance! Since
it was a locking mechanism, lots of fun to debug... So, I suggest never
using self.__class__.attribute, unless you don''t mind it''s children
overriding it, but if you want a truly top-level class attribute, use
ClassName.attribute everywhere!

I wish books and tutorials mentioned this explicitly....

Gabriel

If you define a class instance variable with the same name as the class
attribute, how would Python be able to distinguish the two? That is a feature
not a problem. Getter looks for instance attribute, if one is not found it
looks for a class attribute, and upwards. This behavior is used by Zope to do
all sorts of neat stuff.

-Larry Bates


On 5 juin, 17:40, Gabriel Rossetti <gabriel.rosse...@arimaz.com>
wrote:

Hello everyone,

I had read somewhere that it is preferred to use
self.__class__.attribute over ClassName.attribute to access class (aka
static) attributes.

It''s even prefered to use self.attribute, unless you know you have
both an instance and a class attribute by the same name (but then if
you need to know which one you''re accessing then you have a serious
design problem).

I had done this and it seamed to work, until I
subclassed a class using this technique and from there on things started
screwing up. I finally tracked it down to self.__class__.attribute! What
was happening is that the child classes each over-rode the class
attribute at their level,

Which is why it''s prefered to access the attribute from the class (or
more simply from the instance which will get them from the class) - a
subclass may have extremly good reasons to have it''s own instance of
the attribute.

and the parent''s was never set,

This is another problem.

so while I was
thinking that I had indeed a class attribute set in the parent, it was
the child''s that was set, and every child had it''s own instance! Since
it was a locking mechanism, lots of fun to debug...

I can well believe it, and you do have my whole sympathy.

So, I suggest never
using self.__class__.attribute, unless you don''t mind it''s children
overriding it, but if you want a truly top-level class attribute, use
ClassName.attribute everywhere!

I would not have expressed it that way. My own experience is that,
most of the time, you do know when it''s ok for the subclasses to have
their own instance of the attribute and when it''s not, so in the (very
rare) cases it''s not ok you use __name_mangling.

Now I''d say that *unknowingly* overriding an attribute of your own
base class when you didn''t expect it to happen is mostly a sign that
there''s something you don''t (didn''t ?) quite get wrt/ lookup /
assignment / namespace etc rules in Python.

I wish books and tutorials mentioned this explicitly....

Possibly. OTHO, if you understand Python''s lookup (name resolution)
rules, the difference between MyBaseClass.attrib and
self.__class__.attrib should be obvious. But this surely could be a
good example in a tutorial !-)


"br*****************@gmail.com" <br*****************@gmail.com>
writes:

On 5 juin, 17:40, Gabriel Rossetti <gabriel.rosse...@arimaz.com>
wrote:

>Hello everyone,

I had read somewhere that it is preferred to use
self.__class__.attribute over ClassName.attribute to access class (aka
static) attributes.


It''s even prefered to use self.attribute,

I was going to write exactly that, but it occurred to me that he might
want to be *assigning* to self.__class__.attribute (or
HisClass.attribute) from his methods, in which case self.attribute
would be quite different.


这篇关于ClassName.attribute vs self .__ class __。attribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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