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.
__dict__
__doc__
None
__name__
__module__
In an interactive mode it will give us __main__.
__main__
__bases__
In the following Python program we are creating Awesome class with documentation.
Awesome
# 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.
In 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:
In 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__)
In 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__)
(<class 'object'>,)
In 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__)
{'__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>}