Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly override __setattr__ and __getattribute__ on new-style classes in Python?

I want to override my Python class's __getattribute__ and __setattr__ methods. My use case is the usual one: I have a few special names that I want to handle, and I want the default behavior for anything else. For __getattribute__, it seems that I can request the default behavior simply by raising AttributeError. However, how can I achieve the same in __setattr__? Here is a trivial example, implementing a class with immutable fields "A", "B", and "C".

class ABCImmutable(SomeSuperclass):     def __getattribute__(self, name):         if name in ("A", "B", "C"):             return "Immutable value of %s" % name         else:             # This should trigger the default behavior for any other             # attribute name.             raise AttributeError()      def __setattr__(self, name, value):         if name in ("A", "B", "C"):             raise AttributeError("%s is an immutable attribute.")         else:             # How do I request the default behavior?             ??? 

What goes in place of the question marks? With old-style classes, the answer was apparently self.__dict__[name] = value, but documentation indicates that this is wrong for new-style classes.

like image 728
Ryan C. Thompson Avatar asked Aug 12 '11 15:08

Ryan C. Thompson


People also ask

What is Setattr () used for in Python?

Python setattr() function is used to assign a new value to the attribute of an object/instance. Setattr in python sets a new specified value argument to the specified attribute name of a class/function's defined object.

What is Setattr () used for to access the attribute of the object?

What is setattr() used for? Explanation: setattr(obj,name,value) is used to set an attribute. If attribute doesn't exist, then it would be created. 5.

What does Setattr return?

setattr() Return Value The setattr() method returns None .


2 Answers

It's

super(ABCImmutable, self).__setattr__(name, value) 

in Python 2, or

super().__setattr__(name, value) 

in Python 3.

Also, raising AttributeError is not how you fall back to the default behavior for __getattribute__. You fall back to the default with

return super(ABCImmutable, self).__getattribute__(name) 

on Python 2 or

return super().__getattribute__(name) 

on Python 3.

Raising AttributeError skips the default handling and goes to __getattr__, or just produces an AttributeError in the calling code if there's no __getattr__.

See the documentation on Customizing Attribute Access.

like image 148
Hank Gay Avatar answered Oct 11 '22 05:10

Hank Gay


SomeSuperclass.__setattr__(self, name, value) ?

like image 33
Jeannot Avatar answered Oct 11 '22 07:10

Jeannot