I'm having a bit of a dilemma with Python nested classes. What i'm trying to do is get variables from a function at the top level of class A and use them in the sub class B. Something like this
class A:
def function_A():
value = 5
class B:
def function_B(self):
print (A.value)
This method below works if i turn the variable into a class variable like below but it doesn't do what i want:
class A:
value = 5
class B:
def function_B(self):
print (A.value)
I'm guessing that this isn't possible so can anyone provide a workaround?
The class A has no attribute
value
, because thevalue
variable is defined inside thefunction_A
function. Therefore you don't have to accessvalue
as an attribute of the class, but as a normal variable that you can access from classB
, because it is in the same scope. You have to remove theA.
beforevalue
Example: