Special attributes in python
Special attributes in python are useful for a better understanding of the coding for the learners. special attributes are used to do code documentation while development.
|__doc__||The function’s documentation string or None if unavailable; not inherited by subclasses.|
|__name__||The function’s name.|
|__qualname__||The function’s qualified name.|
|__module__||The name of the module the function was defined in, or None if unavailable.|
|__defaults__||A tuple containing default argument values for those arguments that have defaults, or None if no arguments have a default value.|
|__code__||The code object representing the compiled function body.|
|__globals__||A reference to the dictionary that holds the function’s global variables — the global namespace of the module in which the function was defined.|
|__dict__||The namespace supporting arbitrary function attributes.|
|__closure__||None or a tuple of cells that contain bindings for the function’s free variables. See below for information on the cell_contents attribute.|
|__annotations__||A dict containing annotations of parameters. The keys of the dict are the parameter names, and ‘return’ for the return annotation, if provided.|
|__kwdefaults__||A dict containing defaults for keyword-only parameters.|
Whenever the Python interpreter reads a source file, it does two things:
- sets a few special variables like __name__, and then
- executes all of the code found in the file.
Code Sample for __name__
Let’s use a slightly different code sample to explore how imports and scripts work.
#In school.py: import classroom def main(): print(__name__) print(classroom.__name__) if __name__=="__main__": main()
#In classroom.py: def main(): print(__name__) if __name__=="__main__": main()
When the Python interpreter reads a source file, it first defines a few special variables. In this case, we care about the __name__ variable.
__name__ defines the namespace that a Python module is running in. When Python is run, it will replace __name__ with its namespace.
__name__ = “classroom”
After the special variables are set up, the interpreter executes all the code in the module, If we keep breakpoint in imported function, then we can see the visual code flow
Code Sample for __doc__
A docstring is a string comment, which is used to print the first line after the class or method header.
# In calculator.py class calculator: "This calculator used to do math operation." def __init__(self): pass def add(self): "Inside the class, the function is called the method. These add method will do the addition operation." pass def sub(self): "Inside the class, the function is called the method. This sub-method will do the subtraction operation." pass print(calculator.__doc__) print(calculator.add.__doc__) print(calculator.sub.__doc__) def outside class(): "This is outside of a class, this is function." pass print(outside class.__doc__)
This calculator used to do math operation. Inside the class, the function is called the method. These add method will do the addition operation. Inside the class, the function is called the method. This sub-method will do the subtraction operation. This is outside of a class, this is a function.
Relate to this topic:
Useful Job links:
Useful Book links: