Home > OS >  Setting the value of a class property via method
Setting the value of a class property via method

Time:11-06

So, I have been trying to implement the following:

from abc import ABC
from abc import abstractmethod

class parent(ABC):
    @property
    @abstractmethod
    def example_variable(self)->str:
        ...
        
    @abstractmethod
    def example_method(self)->int:
        ...
        
    def __init__(self, foo: int):
        self.foo = foo
        
class child(parent):
    def example_method(self)->int:
        return self.foo   10
    
    example_variable = f"This is example variable of value {self.example_method()}"
    
if __name__ == "__main__":
    example_object = child(59)
    print(example_object.example_variable)

As you can see, I have created an abstract class with an abstract property and method, and I have tried to implement the child class by using the value of an instance variable to compute a simple value and using that value to set the value of a property using an F string.

But the difficulty is that the interpreter for some reason does not recognise the self reference for the example_method():

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [7], line 17
     14     def __init__(self, foo: int):
     15         self.foo = foo
---> 17 class child(parent):
     18     def example_method(self)->int:
     19         return self.foo   10

Cell In [7], line 21, in child()
     18 def example_method(self)->int:
     19     return self.foo   10
---> 21 example_variable = f"This is example variable of value {self.example_method()}"

NameError: name 'self' is not defined

And I have been trying to figure this out, without success.

I tried removing the self reference:

example_variable = f"This is example variable of value {example_method()}"

But then it throws the error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [10], line 17
     14     def __init__(self, foo: int):
     15         self.foo = foo
---> 17 class child(parent):
     18     def example_variable(self)->int:
     19         return self.foo   10

Cell In [10], line 21, in child()
     18 def example_variable(self)->int:
     19     return self.foo   10
---> 21 example_variable = f"This is example variable of value {example_method()}"

NameError: name 'example_method' is not defined

My question is, why doesn't the python interpreter evaluate the self reference correctly? Has the method been not instantiated yet? If so, then what is the correct way to instantiate the example_method()?

CodePudding user response:

The 'self' argument is only recognized within a class method as it is passed as an argument to the method. (By default, the first argument of a Python class method is a pointer to the class) The example_variable in the above case is being defined outside the method, hence, using self.example_method() will be incorrect.

Secondly, if you are inheriting a parent class within a child class, it must first be initialized with super().__init__() for its methods to be recognized.

My suggestion will be to define example_variable locally within the constructor of the child class or through another method.

CodePudding user response:

I'd say that your problem lies in the fact that what you want is example_variable to be a variable defined during init, so define it in __init__. However, doing so I'm not sure that example_variable won't be fixated with the value of example_method during init; if you want example_variable to change in case the result of example_method changes, then example_variable should probably stay a @property function, just the same way you defined it in your parent class.

  • Related