5

I'm trying to use a variable / list in a function that is defined in another function without making it global.

Here is my code:

def hi():
    hello = [1,2,3]
    print("hello")

def bye(hello):
    print(hello)

hi()
bye(hello)

At the moment I am getting the error that "hello" in "bye(hello)" is not defined.

How can I resolve this?

5
  • as it stands, hello is a name (read variable), local to the hi function. The best way, from what I can see, would be to assign hello in the global space (i.e. where you call the functions) and pass such variable as a parameter to the functions. Or, as suggested below, let hi return hello Commented Oct 28, 2015 at 11:39
  • you can't resolve this, local variable to a function can't be seen in an other : it's all the interest of local variables ! Commented Oct 28, 2015 at 11:42
  • I believe this is an XY problem, since what you are attempting does not adhere to any good practice. What are you trying to achieve? What is the reason for wanting to perform such odd coding? Commented Oct 28, 2015 at 11:47
  • @Pynchia The code I used above was just something thrown together. I'm really just trying to access a list defined in one function in a different function without making it global. Kinda similar to the XY problem :P Commented Oct 28, 2015 at 11:54
  • alright, I thought I might try and help you solve the design conundrum :) Commented Oct 28, 2015 at 12:32

4 Answers 4

5

You need to return hello from your hi method.

By simply printing you are not able to gain access to what happens inside the hi method. Variables created inside a method remain within the scope of that method.

Information on variable scope in Python:

http://gettingstartedwithpython.blogspot.ca/2012/05/variable-scope.html

You return hello inside your hi method, then, when you call hi, you should store the result in a variable.

So, in hi, you return:

def hi():
    hello = [1,2,3]
    return hello

Then when you call your method, you store the result of hi in a variable:

hi_result = hi()

Then, you pass that variable to your bye method:

bye(hi_result)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I tried doing this, but I couldn't remember how to do it. Your answer certainly helped :P
3

if you don't want to use a global variable, your best option is just to call bye(hello) from within hi().

def hi():
    hello = [1,2,3]
    print("hello")
    bye(hello)

def bye(hello):
    print(hello)

hi()

2 Comments

Thanks! This looks like the best method for me to do it. The code in the question was example code which I made up. I tried implementing your idea into my main code, but I am getting the same error for other variables in the function "bye" (Not called bye in my proper code). How can I solve this? @QuakeCore
You can try to send that other variable as an argument along with hello, or yet better to use classes.
2

You cannot declare global variables inside a function without global.You can do this

def hi():
    hello = [1,2,3]
    print("hello")
    return hello

def bye(hello):
    print(hello)

hi()
bye(hi())

Comments

0

As others have said, it sounds like you're trying to solve something that would be better off done a different way (see XY problem )

If hi and bye need to share different types of data, you might be better off using a class. Ex:

class MyGreetings(object):
    hello = [1, 2, 3]

    def hi(self):
        print('hello')

    def bye(self):
        print(self.hello)

You could also do it with globals:

global hello

def hi():
    global hello
    hello = [1,2,3]
    print("hello")

def bye():
    print(hello)

or by having hi return a value:

def hi():
    hello = [1,2,3]
    print("hello")
    return hello

def bye():
    hello = hi()
    print(hello)

or you could have hi put hello on the hi function itself:

def hi():
    hello = [1,2,3]
    print("hello")
    hi.hello = hello


def bye():
    hello = hi.hello
    print(hello)

Now that said, the sketchy way to accomplish what you're asking would be to pull out the source code of hi(), and execute the body of the function within bye() and then pull out the variable hello:

import inspect
from textwrap import dedent


def hi():
    hello = [1,2,3]
    print("hello")

def bye():
    sourcelines = inspect.getsourcelines(hi)[0]
    my_locals = {}
    exec(dedent(''.join(sourcelines[1:])), globals(), my_locals)
    hello = my_locals['hello']
    print(hello)

1 Comment

global hello in the global scope does nothing, because assignments in the global scope already define global variables. (There's just no reason to complicate the grammar by disallowing global statements in the global scope.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.