Python Descriptor Methods
What does these attributes do exactly:
This is a hard one to start with, and fairly advanced - most Python programmers don't need to know this, they just memorize how objects (mostly methods and method decorators) that use this work.
They are special methods that make an object a "descriptor". If that object is looked up through another object, they get invoked. They are how
staticmethod work. The are also how normal functions can become bound methods.
>>> class Foo:
... def __init__(self, bar):
... self.bar = bar
... def baz(self):
... return bar * 2
<method-wrapper '__get__' of function object at 0x7f1c1d23b158>
__get__ is invoked when you do
Foo.__init__ (which is invoked by
Foo()). It binds the instance to the first argument (we usually call it
self) to the method on the dotted lookup.
<method-wrapper '__set__' of property object at 0x7f1c1d225e58>
__set__ method is what is invoked when we try to assign to the
baz name on the dotted lookup on the left hand side of the assignment (since we didn't define a setter, baz is read-only) and that method raises the following error:
>>> f = Foo('bar')
>>> f.baz = 'boink'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
I wrote a canonical and very thorough answer on this subject on StackOverflow here.
Why not use just
Myclass.NewAttr = value instead of
Yes, you should do that.
__setattr__ should only be used when implementing special behavior on an object.
There is also a builtin function called
setattr - it works if you need to programmatically determine the name you are assigning to, and can't just use a static name in your code.
Why does Python use
__dict__? It keeps the attributes in the end of
Actually, the attributes (data members) are stored in the dictionary -
dir() just looks their names up there.