0

Is there any way to get this to work?

class A:
    def method(self):
        x = 5
        y = 2

        method.z = x * y
        

class B(A):    
    def method(self):
        super().method()

        z = super().method().z
        xyz = 2**z


def main():
    a = A().method()
    b = B().method()


main()

Running it gives

NameError: name 'method' is not defined

The idea is that the super class does preliminary work that all subclasses would need to perform, and passes the result on to the subclass for use (in this case z).

Because the data is only needed in the scope of the subclass method calls, it doesn't make sense to store it in the scope of the class as an instance variable.

The above code is using notation found here, in search of C static function variables in python.

young_souvlaki
  • 1,886
  • 4
  • 24
  • 28
  • Make it an attribute (ie. `self.x`). – SuperStormer Apr 15 '21 at 23:06
  • Your `__init__` didn't define any instance attributes, and variables local to `A.__init__` work like any other local variable, and are not accessible outside the function (unless they become part of a closure) – juanpa.arrivillaga Apr 15 '21 at 23:08
  • I don't want it to live in the class, only in the method. Does python have something similar to a C static variable where it is defined once and lives for all subsequent function calls? – young_souvlaki Apr 15 '21 at 23:11
  • 3
    That sounds seriously weird. Why do you want to do that? – user2357112 Apr 15 '21 at 23:15
  • 1
    (I know what C's static variables are, but using them like this would be bizarre.) – user2357112 Apr 15 '21 at 23:15
  • 1
    Sounds like a XY problem – SuperStormer Apr 15 '21 at 23:16
  • 1
    @young_souvlaki no, Python is very much not like C. The *entire purpose* of an instance variable is to share state within a class. What you are describing should be an instance variable in Python. Just name it with a single-underscore to e.g. `self._x = 5` if it isn't part of the public API – juanpa.arrivillaga Apr 15 '21 at 23:18
  • I updated the sample code. I don't think an instance variable is the answer. I want to write most of the functionality for a method, and have the child classes extend the method and use the parent class calculations to do what they need. I'll try to add this into the sample code. – young_souvlaki Apr 15 '21 at 23:23
  • 1
    "Because the data is only needed in the scope of the subclass method calls, it doesn't make sense to store it in the scope of the class as an instance variable." - that sounds like a case for an instance variable, and *absolutely not* for anything like a C static variable - a static variable would be shared across instances, causing problems with reentrancy and thread safety. – user2357112 Apr 15 '21 at 23:36
  • 1
    Or with the edit, since this isn't `__init__`, it sounds like `A`'s method should just *return* the data it needs to communicate to its caller. – user2357112 Apr 15 '21 at 23:37
  • "Because the data is only needed in the scope of the subclass method calls, it doesn't make sense to store it in the scope of the class as an instance variable." yes, it *does* make sense to store it as instance state, because it is instance state. The approach you are *trying* approach this now with a function attribute won't work with instances as methods cleanly, and even if it *did* it is effectively using a class-variable anyway. Doing this any other way than an instance variable would be highly unidiomatic, unexpected, and convoluted – juanpa.arrivillaga Apr 15 '21 at 23:41
  • Yea I see your point about the static variable in this context. Definitely do not want it shared across instances. It seems like I just want a method with a return value, since I don't want to store the preliminary data. What about having something like `z = super().method()` in the subclass? Would it be better to have a helper function with a separate name? – young_souvlaki Apr 16 '21 at 00:58

0 Answers0