In that question on META I have summarized the content of one of my question which I have written on SoftwareEngineering, but deleted because it has immediately received 2 downvotes (I have gained a Peer Pressure badge :) but I'm not so happy for this because my doubt remains not satisfied).
@Thomas Owens explained carefully why my question is not suited for SoftwareEngineering, but suggest me to write another question here on Meta.SWE which shows the whole original question and not just a summary. He explained me that in this way someone could help me to improve the question.
My question here in Meta.SoftwareEngineering
I thank in advance anyone who wants to help me improve the following question so that it can be suitable for SoftwareEngineering or explain to me that it is not exactly the right site and perhaps tell me where to publish the question.
So below I shows my originally question (topics of the question are Python, class and OOP):
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):
import unittest from unittest import mock class A: def __f1(self): pass def __f2(self): pass def __f3(self): pass def method_1(self): self.__f1() self.__f2() self.__f3() class MyTestCase(unittest.TestCase): def test_call_order(self): mock_a = mock.create_autospec(A) expected = [mock.call._A__f1(), mock.call._A__f2(), mock.call._A__f3()] # call method_1 by reference A.method_1(mock_a) self.assertEqual(expected, mock_a.mock_calls) if __name__ == '__main__': unittest.main()
I generally create an instance of a class and invoke the methods of the class by this instance object. So I could simply write
mock_a.method_1()) instead of
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.
Example of such an attribute reference use case will be appreciated.