Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Section B.6. Attribute access

B.6. Attribute access

In Python you can completely customize attribute access on objects for fetching, setting, and deleting. Table B.6 shows the attribute access methods.

Table B.6. Attribute access methods
MethodDescription
__getattr__(self, name)If this method is implemented, then it will be called with the attribute name when an attribute that doesn’t exist on the object is requested. If fetching the attribute fails, it should raise an AttributeError.
__setattr__(self, name, value)If this method is implemented, it will be called whenever an attribute is set on an instance. __setattr__ can do “real” attribute setting by delegating to object: object.__setattr__(self, name, value).
__delattr__(self, name)If this method is implemented, it will be called whenever an attribute is deleted.
__getattribute__(self, name)This method is always called for attribute access (new-style classes only). It is an important part of the descriptor protocol. It should return the attribute or raise an AttributeError. If both this method and __getattr__ are explicitly defined, then __getattr__ will be called only if __getattribute__ calls it.



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial