1

I have a few more related OOP Python questions:

  • What does these attributes do exactly: __get/set/delattr__?

  • Why not use just Myclass.NewAttr = value instead of __setattr__?

  • Why does Python use __dict__? It keeps the attributes in the end of dir() anyway.

1 Answer 1

2

Python Descriptor Methods

What does these attributes do exactly: __get/set/delattr__?

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 property, classmethod, and staticmethod work. The are also how normal functions can become bound methods.

For example:

>>> class Foo:
...     def __init__(self, bar):
...         self.bar = bar
...     @property
...     def baz(self):
...         return bar * 2
>>> Foo.__init__.__get__
<method-wrapper '__get__' of function object at 0x7f1c1d23b158>

The __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.

>>> Foo.baz.__set__
<method-wrapper '__set__' of property object at 0x7f1c1d225e58>

That __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.

Assignment versus __setattr__ (versus setattr)

Why not use just Myclass.NewAttr = value instead of __setattr__?

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.

__dict__ versus dir

Why does Python use __dict__? It keeps the attributes in the end of dir() anyway.

Actually, the attributes (data members) are stored in the dictionary - dir() just looks their names up there.

3
  • cool. some artical show example of use the methods get/set/delattr and i still can't figure out when to use them. by the way, i dont understand why you have to use super in those methods....
    – Adi Gal
    Commented Mar 10, 2016 at 13:31
  • Well, descriptors are an advanced topic that most Python programmers don't really need to know about - I haven't had the need to write my own, even. For more on super - I have pretty comprehensive answers here: stackoverflow.com/questions/576169/… and here: stackoverflow.com/questions/222877/how-to-use-super-in-python/…
    – Aaron Hall
    Commented Mar 10, 2016 at 13:35
  • i think i get it.. set/get/delattr is methods under "object" (new-style-class), it's very interesting what actually happens when you create new object - when you call the init function - you actually call to the setattr function.. and so.. do you have anything you wrote about what happens behind the scene ? for set get mro multi inhibits (bases,classes) and so?.. i know that most of Python programmers don't need to know this. but i'm really interesting about it. thanks
    – Adi Gal
    Commented Mar 10, 2016 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.