-1

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.

Rohan Singh
  • 461
  • 2
  • 5
  • 20
  • make `func(x, y)` and call it with your `y` variable – Andrej Kesely Jul 24 '18 at 15:31
  • 1
    The `global` statement is used to declare a name being 'not local' in a function that alters it. You put it in the wrong place. – Martijn Pieters Jul 24 '18 at 15:31
  • 1
    What you *really* should do, however, is pass in `y` as an argument to `func()`. Why do you think you need a global in the first place? – Martijn Pieters Jul 24 '18 at 15:32
  • Put differently: any name *not* being bound to (assignment is one form of binding) is *already a closure or a global*. `y` doesn't get bound to in `func()`, it thus a global name. `y=4` binds `y` in `forward()`, making it local, automatically. `global y` changes the scope there, because it is there that it matters. – Martijn Pieters Jul 24 '18 at 15:34
  • @MartijnPieters Should i delete this question since it seems the answer lies in a different post? – Rohan Singh Jul 24 '18 at 15:45
  • You can't, because there is an upvoted answer (which has a strange, incorrect first paragraph, at least in this context), that you marked accepted. And you don't need to, either, duplicate posts *can* help future visitors find the canonical post on the subject. – Martijn Pieters Jul 24 '18 at 15:48

1 Answers1

3

You can't. A member variable belongs to a specific instance of a class. You have to know the instance to do that, so you'd better pass y to your function as well.

You could do the opposite though. use global in the forward function to define a global y variable, and then access it from the func. However, don't. It's bad practice, always. Pass y to func instead.

blue_note
  • 27,712
  • 9
  • 72
  • 90
  • This has nothing to do with member variables. a) that's not a concept in Python. b) if you are talking about instance attributes, then there is no `self.y` involved here. It's *just another local*. Methods are no different here from regular functions. – Martijn Pieters Jul 24 '18 at 15:35
  • Put differently: **you can do what the OP wants, trivially**, but the OP shouldn't. – Martijn Pieters Jul 24 '18 at 15:36
  • How can I define a global variable in a method? btw, I know what I'm asking sounds silly, but I need it for a larger program I am writing. – Rohan Singh Jul 24 '18 at 15:40
  • 1
    @RohanSingh: Just put `global y` before `y = 4`. the `global` keyword means: when I refer to `y`, use the global one, if it exists, or create it, if it doesn't – blue_note Jul 24 '18 at 15:40
  • @RohanSingh: Methods are just functions. They are not special when it comes to scoping rules. See the duplicate, and my comments. Put `global y` *in the `forward` function body*. – Martijn Pieters Jul 24 '18 at 15:40
  • Ok thank you for your answers, I guess this question boiled down to creating a global variable in a method. – Rohan Singh Jul 24 '18 at 15:43
  • 1
    @RohanSingh: ok, creating a global variable is the same, whether in a function or a method – blue_note Jul 24 '18 at 15:44