1

I'm writing this question to follow the suggestion that @Thomas Owens has given me by a comment added to a his answer to one my question.

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

3
  • 1
    Well, I guess Thomas Owens' answer was based on the assumption you literally wanted to know why specifically Python has a certain feature. But it seems you really want to know what's the point of having the option of accessing attribute references of a class without an instance of the class (not really specifically for Python). Did I get that correct?
    – Doc Brown
    Nov 9, 2023 at 15:21
  • @DocBrown Yes you are right. For a period, before start programming in Python I have used Java which (if I remember), without an instance of a class, allows to use only static methods. In Python I can access to all the methods of a class without an instance. On my opinion this is the opposite of OOP and creates in me a bit of confusion.
    – User051209
    Nov 9, 2023 at 15:45
  • In Java, the corresponding feature can be achieved by reflection.
    – Doc Brown
    Nov 9, 2023 at 15:47

1 Answer 1

1

IMHO your question is almost fine. I made some improvements to the grammar, and added a few words to the final part (please double check if I got your intentions right).

Still, some of our community members will probably downvote it, since it asks for example use cases, and questions for 'some examples' seem to be a red flag for some of our members, they might downvote and close vote for it (not off-topic, but "needs more focus"). That's why I tried to reword the core question a little bit and make sure the "what's the point" part is in front, and the "examples" part just in the background.

My recommendation: just ask it now, and when it gets some downvotes, at least me will give an upvote, which will balance your score.

5
  • Thank you I have ask the question an other time.
    – User051209
    Nov 9, 2023 at 15:54
  • Community-hardliners can't stop to downvoting.. To avoid this I'll spend more time to read post here on SE before ask something else. Thank you very much for all.
    – User051209
    Nov 10, 2023 at 8:03
  • 1
    @frankfalse: it is surely a good idea to invest time into research before asking here, but note that 90% of all questions asked on this site get a downvote, and most of them uncommented. I am sure most of those downvotes come from a very small group within the community, with at least one or two persons belonging to the autistic spectrum. Those are quite obsessive in following the site rules 100% literally, and whenever they find a wording which could be interpreted in some way it might violate any of the on-topic rules, they downvote....
    – Doc Brown
    Nov 10, 2023 at 8:47
  • 1
    ... so don't take this personal, it's not your fault when you ask a good question and it gets still downvotes.
    – Doc Brown
    Nov 10, 2023 at 8:49
  • However the answers and comments that I have received have been very useful for me and now I have increased a bit my knowledge.
    – User051209
    Nov 10, 2023 at 8:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .