Attribute references and instantiation
In this link, that is part of the official Python documentation, I have found the following information:
Class objects support two kinds of operations: attribute references and instantiation.
Attribute references use the standard syntax used for all attribute references in Python. So if
MyClassis the name of a class and
funcis the name of one attribute of
MyClass.funcis a valid attribute reference.
Class instantiation uses function notation. So
x = MyClass()creates a new instance of the class and assigns this object to the local variable
At the beginning of previous documentation the expression
Class object is used and this means that in Python a class is an object and
on this object we can execute only 2 operations and one of these is attribute references.
Up to now I have used the Attribute references only in one case to access a method of a class: for writing a unit-test which verified the exact sequence of method calls.
I show a simplified code of the test below (for details see here):
from unittest import mock
mock_a = mock.create_autospec(A)
expected = [mock.call._A__f1(),
# call method_1 by reference
if __name__ == '__main__':
I generally create an instance of a class and invoke the methods of the class by this instance object.
On my opinion it's better to use a python module which contains functions and global variables than create a class and access its attributes by reference.
When or why can it be useful to access the attributes of a class by Attribute references and without an instance of that class? What is the point of this language feature? I have a hard time to imagine a case where attribute references make sense without an instance.
An example where it is absolutely necessary the use of such an attribute reference (as in my example) will be appreciated.