Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of "global" keyword in Python

What I understand from reading the documentation is that Python has a separate namespace for functions, and if I want to use a global variable in that function, I need to use global.

I'm using Python 2.7 and I tried this little test

>>> sub = ['0', '0', '0', '0'] >>> def getJoin(): ...     return '.'.join(sub) ... >>> getJoin() '0.0.0.0' 

It seems things are working fine even without global. I was able to access global variable without any problem.

Am I missing anything? Also, following is from Python documentation:

Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement.

While formal parameters and class definition make sense to me, I'm not able to understand the restriction on for loop control target and function definition.

like image 507
nik Avatar asked Jan 14 '11 16:01

nik


People also ask

What is the use of global keyword?

A global keyword is a keyword that allows a user to modify a variable outside of the current scope. It is used to create global variables in python from a non-global scope i.e inside a function.

What is a global variable in python?

Variables that are created outside of a function (as in all of the examples above) are known as global variables. Global variables can be used by everyone, both inside of functions and outside.

Is it good to use global in Python?

Specifically to python, globals' visibility is limited to a module, therefore there are no "true" globals that affect the whole program - that makes them a way less harmful. Another point: there are no const , so when you need a constant you have to use a global.

What is global name in Python?

global means “a name at the top-level of a module file.” Global names must be declared only if they are assigned in a function. Global names may be referenced in a function without being declared.


1 Answers

The keyword global is only useful to change or create global variables in a local context, although creating global variables is seldom considered a good solution.

def bob():     me = "locally defined"    # Defined only in local context     print(me)  bob() print(me)     # Asking for a global variable 

The above will give you:

locally defined Traceback (most recent call last):   File "file.py", line 9, in <module>     print(me) NameError: name 'me' is not defined 

While if you use the global statement, the variable will become available "outside" the scope of the function, effectively becoming a global variable.

def bob():     global me     me = "locally defined"   # Defined locally but declared as global     print(me)  bob() print(me)     # Asking for a global variable 

So the above code will give you:

locally defined locally defined 

In addition, due to the nature of python, you could also use global to declare functions, classes or other objects in a local context. Although I would advise against it since it causes nightmares if something goes wrong or needs debugging.

like image 165
unode Avatar answered Sep 30 '22 18:09

unode