
Python
In this tutorial we will learn about built-in class attributes in Python.
Built-in class attributes gives us information about the class.
We can access the built-in class attributes using the . operator.
Following are the built-in class attributes.
| Attribute | Description |
|---|---|
__dict__ | This is a dictionary holding the class namespace. |
__doc__ | This gives us the class documentation if documentation is present. None otherwise. |
__name__ | This gives us the class name. |
__module__ | This gives us the name of the module in which the class is defined. In an interactive mode it will give us |
__bases__ | A possibly empty tuple containing the base classes in the order of their occurrence. |
__doc__ class attributeIn the following Python program we are creating Awesome class with documentation.
# class
class Awesome:
'This is a sample class called Awesome.'
def __init__(self):
print("Hello from __init__ method.")
# class built-in attribute
print(Awesome.__doc__)
The above code will give us the following output.
This is a sample class called Awesome.
__name__ class attributeIn the following example we are printing the name of the class.
# class
class Awesome:
'This is a sample class called Awesome.'
def __init__(self):
print("Hello from __init__ method.")
# class built-in attribute
print(Awesome.__name__)
Output:
Awesome
__module__ class attributeIn the following example we are printing the module of the class.
# class
class Awesome:
def __init__(self):
print("Hello from __init__ method.")
# class built-in attribute
print(Awesome.__module__)
Output:
__main__
__bases__ class attributeIn the following example we are printing the bases of the class.
# class
class Awesome:
def __init__(self):
print("Hello from __init__ method.")
# class built-in attribute
print(Awesome.__bases__)
Output:
(<class 'object'>,)
__dict__ class attributeIn the following example we are printing the dict of the class.
# class
class Awesome:
def __init__(self):
print("Hello from __init__ method.")
# class built-in attribute
print(Awesome.__dict__)
Output:
{'__module__': '__main__', '__doc__': 'Awesome class sample documentation.', '__init__': <function Awesome.__init__ at 0x106e2c1e0>, '__dict__': <attribute '__dict__' of 'Awesome' objects>, '__weakref__': <attribute '__weakref__' of 'Awesome' objects>}
ADVERTISEMENT