Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a variable from outer scope via global keyword in Python

As per this Question's accepted answer, I understand I cannot create pure global variables. Okay, cool.

However, he then goes on and says:

[..]all you can do is make a variable in a particular scope. (If you make a variable inside the Python interpreter, and then import other modules, your variable is in the outermost scope and thus global within your Python session.[..]

Okay, so we can't assign globals in the original sense, but it appears to be possible to access variables in the outermost scope from within a package, via the global keyword, correct?

I am then, apparently, missing something crucial in my efforts to access a variable passed to my python program via commandline arguments.

My program has the usual __main__.py, which handles argument parsing and executes code from my python module backend.

backend has code that relies on input via command line arguments. However, I seem to fail at making these arguments available to backend.

My package's layout is:

mypackage
- __main__.py
- backend/
    -__init__.py
    -direction.py

Here's __main__.py:

import argparse

# Setup ArgParser
parser = argparse.ArgumentParser(description="Fancy Description of program.")
parser.add_argument('--tar', nargs=1, dest='target',
                help='Specify output folder.',
                default=['/path/to/output'])

target_dir = args.target[0]

# Import backend now that our variable is set.
from backend.direction import direction

And my backend/direction.py:

global target_dir
print(target_dir)

Running this raises a NameError: 'target_dir' is not defined. So where's the poop ? Am I assuming an impossibility here, or am I simply messing up on declaration?

like image 506
deepbrook Avatar asked Jun 10 '16 08:06

deepbrook


People also ask

How do you access the outer scope of a variable in Python?

It is not defined within that function. However, you can access first_num from inner() (# Print statement 1), because the scope of first_num is larger, it is within outer() . This is an enclosing scope. Outer 's variables have a larger scope and can be accessed from the enclosed function inner() .

Can Python access global variables?

You can access global variables in Python both inside and outside the function.

What happens when you try to access a variable outside of its scope?

It will throw an error if you try to access a variable which is not in the local or global scope.

What is global keyword in Python?

In Python, global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.


Video Answer


1 Answers

global only works within the module it's used in. You also don't use global to declare a global variable at the global scope (ie. the module scope). You would use global within the scope of a function to indicate that you want to use a variable in the global scope and not in the local scope.

In python 3, there is also the nonlocal keyword that allows you to indicate you want to modify a variable in an outer scope that isn't the global/module scope.

global

A = 1

def global_func():
    global A
    A = 2


def func():
    A = 3


print(A)
# 1

global_func()
print(A)
# 2

func()
print(A)
# 2

nonlocal

def outside():
    a = 1

    def inside_nonlocal():
        nonlocal a
        a = 2

    def inside():
        a = 3

    print(a)
    # 1

    inside_nonlocal()
    print(a)
    # 2

    inside()
    print(a)
    # 2
like image 173
Brendan Abel Avatar answered Oct 10 '22 07:10

Brendan Abel