2

I'm trying to access a variable of a method in another method of the SAME class and for some reason it prints nothing.

class Colors:
    def blue(self):
        var = "This is blue"
           
    def red(self):
        b = self.blue
        print(b.var)

I've also tried print(self.blue.var).

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
user652124
  • 51
  • 2
  • 3

5 Answers5

10

Local variables are, well, local to the scope of a function. They don't even exist when the function is not currently being executed, so there is nothing to access.

If you want the variable to have a bigger scope, well, just give it a bigger scope:

def blue(self):
    self.var = "This is blue"

def red(self):
    print(self.var)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
3

Only one method runs at a time (in the simplest case, which is the default and the most common in most languages), and since local variables only exists while a function is running, i.e. get created when the function is called and destroyed when it returns, there is no such local during the execution of red.

Now, when you add concurrency, it's possible that red and blue run at the same time (but normally, you'd never know nor should you care). But var is still a local variable of blue, so even if there was some heck to get its value, doing so would be utterly insane and a crime against good practice. Sharing state between threads via globals is enough of a pitfall, no need to throw the locals in the mix as well.

What are you really trying to do? You propably want an instance variable (self.var = ...) or return something from blue.

  • I was of thinking of using an existing variable, save me coding time. I can work my way around this, but just wanted if it is possible what im trying to do in the initial code i posted. – user652124 Mar 14 '11 at 17:49
1

Local variables in a function/method cannot be accessed outside that function/method. To share state between methods, use an instance variable.

class Colors:
    def blue(self):
        self.var = "This is blue"

    def red(self):
        print(self.var)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
kindall
  • 178,883
  • 35
  • 278
  • 309
1

You have a couple of unexpected things going on here:

  1. You are assigning b to self.blue, which is a function pointer to blue.

  2. You have a scope issue, you want the following

    def blue(self):
        self.var = "This is blue"
    
    def red(self):
        print(self.var)
    

Or if you want to print something that blue is returning you do:

def blue(self):
    return "This is blue"

def red(self):
    print(self.blue())
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
0

Thanks guys. I worked a way around it. made another local variable of the same value on the 2nd method.

user652124
  • 51
  • 2
  • 3
  • Please consider accepting the answer the helped you the most (click the green check mark by the answer). This indicates to everyone that this is the solution you chose. – Bill the Lizard Mar 14 '11 at 22:28