0

In python we can declare a global variable which will be accessible by other functions.

my_global_var = 2

# foo can only access the variable for reading
def foo():
    print(my_global_var)

# bar can also write into the global variable
def bar():
    global my_global_var
    print(my_global_var)

This works, however suppose that I don't want to create the global variable outside foo and bar, but instead I want foo to create a local variable and extend the scope of this variable to bar (and any other function) without passing it as a parameter.

Something like

def foo():
    # the scope of this variable is only foo. Can I make it global?
    my_global_var = 4  

# bar wants to be able to access (and maybe modify) the variable created by foo
def bar():
    global my_global_var
    print(my_global_var)

PD: For the comments I think my question is not understood.

It is clearly not a duplicate of that other question, since I know how to use global variables (the first example in the question uses them).

And I'm also not asking for suggestions about passing the variables as parameters or not using global variables.

My question is very specific. Can I extend the scope of a local variable into a global variable?

It's either yes, it can be done this way or No, this cannot be done. And if the answer is yes I would like to know how can be done.

  • what's wrong with "passing it as a parameter"? – Chris_Rands Dec 14 '18 at 15:02
  • My code is event oriented. Some variables will only be created if some event happens and other functions will only access those variables if they exist. –  Dec 14 '18 at 15:05
  • Possible duplicate of [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function). Note that using globals is considered bad practice – buran Dec 14 '18 at 15:05
  • 2
    I would define it just like your `my_global_var` with a default value and let foo be able to change its value when it is called. Therefore you will not have to check if it is already initialized in bar – Jonathan R Dec 14 '18 at 15:06
  • @JonathanR this is exactly what I'm doing in the first example, I am already doing this and aware of that. My question asks if it's possible to extend the scope of a local variable created inside a function to a global variable acccessible by other functions. –  Dec 14 '18 at 15:34
  • 1
    There is no situation where you would want a local variable to become a global one. Variables always need a scope. Your only option is to define them globally. – Jonathan R Dec 14 '18 at 15:42

1 Answers1

0

There are any number of ways to work around this and sort of kind of emulate it, but really… No, there is no way to do exactly this.

Scopes also define lifetimes. A variable within a function scope can only exist while the function is running. As soon as the function is done and its scope is destroyed, the variable ceases to exist. If you want the variable to continue to exist, it must live in a scope which also continues to exist. You could nest both functions inside another function to get that effect to some degree, but most likely you'll want to use the global scope for this, which always continues to exist.

deceze
  • 510,633
  • 85
  • 743
  • 889