I am trying to make a function that will be able to access a variable from outside the function. However, this variable needs to be defined in a class. I'll define a simplified function of what I am trying to do in code for clarity.
class Stuff():
def __init__(self):
print("Initialized")
def forward(self,x):
y=4
x=func(x)
return x
def func(x):
global y
return x+y
stuff = Stuff()
print(stuff.forward(4))
So, I am trying to make func(x) use the y defined in the "forward" method, but when I run this code, I get the error "global name y is not defined". Any help is much appreciated.