Skip to main content
added 2 characters in body
Source Link

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.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.

ExampleAn example where it is absolutely necessary the use of such an attribute reference use case where it is absolutely necessary (as in my example) will be appreciated.

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.

Example of such an attribute reference use case where it is absolutely necessary (as in my example) will be appreciated.

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.

An example where it is absolutely necessary the use of such an attribute reference (as in my example) will be appreciated.

Became Hot Network Question
added 52 characters in body
Source Link

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 MyClass is the name of a class and func is the name of one attribute of MyClass then:
MyClass.func is 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 x.

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 A.method_1(mock_a)
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.

My question

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 where it is absolutely necessary (as in my example) will be appreciated.

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 MyClass is the name of a class and func is the name of one attribute of MyClass then:
MyClass.func is 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 x.

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 A.method_1(mock_a).

My question

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.

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 MyClass is the name of a class and func is the name of one attribute of MyClass then:
MyClass.func is 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 x.

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.
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.

My question

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 where it is absolutely necessary (as in my example) will be appreciated.

edited title
Link

Access Why access the attributes of a Python class by reference?

Source Link
Loading