Skip to Content
Mastering Object-oriented Python
book

Mastering Object-oriented Python

by Steven F. Lott
April 2014
Beginner to intermediate
634 pages
15h 22m
English
Packt Publishing
Content preview from Mastering Object-oriented Python

Using special methods for attribute access

We'll look at the three canonical special methods for attribute access: __getattr__(), __setattr__(), and __delattr__(). Additionally, we'll acknowledge the __dir__() method to reveal attribute names. We'll defer __getattribute__() to the next section.

The default behavior shown in the first section is as follows:

  • The __setattr__() method will create and set attributes.
  • The __getattr__() method will do two things. Firstly, if an attribute already has a value, __getattr__() is not used; the attribute value is simply returned. Secondly, if the attribute does not have a value, then __getattr__() is given a chance to return a meaningful value. If there is no attribute, it must raise an AttributeError exception. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Object-Oriented Python - Second Edition

Mastering Object-Oriented Python - Second Edition

Steven F. Lott

Publisher Resources

ISBN: 9781783280971Supplemental Content