Python has multiple special attributes that are defined per default for each class such as __name__
, __module__
, __dict__
, __bases__
, __doc__
, and __annotations__
.
Each of these special attributes has a special meaning as shown in the following table:
Attribute | Type | Description |
---|---|---|
__name__ | str | The name of the class |
__module__ | str | The string name of the module where the class is defined |
__dict__ | dict | The dictionary with the namespace of the class |
__bases__ | tuple | A tuple with base classes of this class |
__doc__ | str or None | The documentation of the class as a string. If no documentation is defined, None . |
__annotations__ | dict | A dictionary with variable annotations in this class |
Let’s dive into a number of examples to show how these special attributes work.
Python Special Attribute __name__
Python’s special attribute __name__
is a string that defines the name of the class.
A minimal example can be seen in the interactive Python shell mode:
>>> __name__ '__main__'
The current name of the module in which this code runs is '__main__'
.
If you would import a module with say import my_modul
e and my_module
would print(__name__)
, the result would be the name 'my_module'
and not '__main__'
.
Related Article:
Python Special Attribute __module__
Python’s special attribute __module__
prints the string name of the module where the class is defined.
Here’s an example:
class My_Class: def __init__(self): print(__name__) My_Class().__module__ # __main__
For this example, I copied this code into a file 'code.py'
and executed this code file as the main module. That’s why the output is '__main__'
.
Python Special Attribute __dict__
Python’s special attribute __dict__
contains the dictionary with the namespace of the class. The dictionary maps the name of the attribute to its specific instantiation.
The following example shows the __dict__
contents of a custom class Person
with two attributes name
and age
and their specific values 'alice'
and 23
, respectively.
class Person: def __init__(self, name, age): self.name = name self.age = age alice = Person('alice', 23) print(alice.__dict__) # {'name': 'alice', 'age': 23}
Python Special Attribute __bases__
Python’s special attribute contains a tuple with base classes of this class.
The following example defines a Child
class with two parent classes Parent
and Person
. If you print the __bases__
attribute, it shows you a tuple with two references to these base classes:
class Parent: pass class Person: pass class Child(Parent, Person): pass print(Child.__bases__) # (<class '__main__.Parent'>, <class '__main__.Person'>)
Feel free to dive deeper into the concepts of OOP inheritance in our article:
Python Special Attribute __doc__
Python objects have an attribute called __doc__
that provides a documentation of the object. For example, you simply call Dog.__doc__
on your class Dog
to retrieve its documentation as a string.
class Dog: """Your best friend.""" def do_nothing(self): pass print(Dog.__doc__) # Your best friend.
More here:
Python Special Attribute __annotations__
Annotations are defined in PEP 3107 allow you to add arbitrary metadata to the parameters and return values of functions.
The __annotations__
attribute of a function object stores those annotations in a dictionary mapping function parameters or the return value to the specified annotations.
def save(x: "starting capital", y: "annual return (e.g., 0.1 == 10%)", n: "number of years"): return x * (1+y)**n # Investing $10,000 at 10% for 50 years print(save(10000, 0.1, 50)) # 1173908.5287969578 # = $1.1 million USD
You can print the annotations using the __annotations__
attribute of the function save
:
print(save.__annotations__) # {'x': 'starting capital', 'y': 'annual return (e.g., 0.1 == 10%)', 'n': 'number of years'}
More here: