O'Reilly logo

Python Unlocked by Arun Tigeraniya

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Playing with attributes

Key 8: Which attribute will be used.

Attributes are values that are associated with an object that can be referenced by name using dotted expressions. It is important to understand how attributes of an object are found. The following is the sequence that is used to search an attribute:

  1. If an attribute is a special method, and it exists in the object's type (or bases), return it, for example: __call__, __str__, and __init__. When these methods are searched, their behavior is only in the instance's type:
    >>> class C: ... def __str__(self,): ... return 'Class String' ... def do(self): ... return 'Class method' ... >>> c = C() >>> print(c) Class String >>> print(c.do()) Class method >>> def strf(*args): ... return 'Instance ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required