Python: self vs type(self) and the proper use of class variables

无人久伴 提交于 2019-12-04 20:56:02

You'll probably want to access them with the actual type name, rather than either self or type(self).

The effect you're seeing has nothing to do with mutability. If you were to do

class Foo(object):
    x = []
    def __init__(self):
        self.x = [1]

the assignment in __init__ would create a new list and assign it to the instance attribute x, ignoring the class attribute, even though Foo.x is a mutable object. Whenever you want to assign to an attribute, you need to use the object it was actually defined on.

Note that modifying the attribute through type(self) fails in the case of inheritance:

class Foo(object):
    x = 1
    def modify_x(self, x):
        type(self).x = x

class Bar(Foo): pass

Foo().modify_x(2) # modifies Foo.x
Bar().modify_x(3) # modifies Bar.x, the wrong attribute
python -c 'import this'
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

I will suggest these for reference

Beautiful is better than ugly.
Simple is better than complex.
Readability counts.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
If the implementation is hard to explain, it's a bad idea.

and my suggestion is to use direct attribute reference because it is probably what the language designers intended.

After speaking with others offline (and per @wwii's comment on one of the answers here), it turns out the best way to do this without embedding the class name explicitly is to use self.__class__.attribute.

(While some people out there use type(self).attribute it causes other problems.)

https://bitbucket.org/larry/cpython350/pull-requests/15/issue-24912-prevent-class-assignment/diff

In common cases they work the same (probably you should use YouClass.NameAttribute to avoid problems with later inheritance). In short the difference was till Python 3.5. The problem was issue #24912 (not allowing assingment in all cases). Example: an immutable type like int was alocated staticalli, and HEAPTYPE rules accidentally where stoping from alowing class assingment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!