0
def foo():
    global a 
    a = 10

vs

a = 0
def foo():
    a = 10

What is the use of declaring global in a function instead of declaring variables right at the top. It should be cleaner this way ?

2
  • Have you tried the 2nd case? What is a after running foo? Commented Mar 23, 2016 at 6:17
  • So the difference between the two cases is that in the first one you change a global variable inside the function. In the 2nd case you don't, a is just a local variable when inside the function, so there is room for confusion. Commented Mar 23, 2016 at 6:19

6 Answers 6

1

Q. What is the use of declaring global in a function instead of declaring variables right at the top?

Generally we define few regularly used constants as global variables.

>>> import math
>>> math.pi
3.141592653589793

Mathematical contants/Web Page/Servernames/URLs - which wont change over a period of time.

In general, it is not recommended to RE-DEFINE global variable or define global variables with in a function. But few exception cases are there where we are left with no option but to re-define it. Could be due to a new upgrade in old system where this variable is heavily used across.

So please go with your second approach. Define 'a' outside your function. This also looks good & easy to read.

>>> a = 10
>>> def foo():
...     a = 14 # defining a local variable called a
...
>>> a
10
>>>
>>> foo()
>>> a  
10
>>> # a = 14 has no scope outside foo function
>>> 
>>> def foo():
...     global a # notify function to use global variable here.
...     a = 15 # re-defining global ==> nt preferred it
...
>>> a
10
>>> foo()
>>> a # see now it changed here.
15

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

In the first case the value remains same in all the functions.

def set_globvar_to_one():
    global globvar
    globvar = 1
    print globvar

def print_globvar():
    print globvar     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()

The output will be 1 and 1.

But 2nd case

def set_globvar_to_one():
    globvar = 1
    print globvar

def print_globvar():
    print globvar     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()

Here you get error saying globvar is not defiend, coz there is no local or global variable named globavar

Comments

1

That would make it ambiguous. Look at this,

a = 0
def foo():
    a = 10 # *

Look at the line marked *. Are you assigning to the global a or is this a local variable a with a value 10. The interpreter has no way to knowing what you meant. Thus, the need for global.

a = 0
def foo():
    a = 10 # local variable a

def bar():
    global a # will be modifying global variable a in this function
    a = 10 # assign 10 to global variable 

Comments

1

In your first example "a" is not available before foo() is called. Once it is defined in the function it available outside of the function.

In the second example it is available before foo() is called butchanges to it inside the function do not affect the global version .

This example should demonstrate the difference between the two examples

#!/usr/bin/python

def foo():
    global a
    a = 10
    print a

try:
 print a
except Exception,e:
 print e

foo()
print a

print "-"*70

b = 0
def bar():
    b = 10
    print b

print b
bar()
print b

Output:

global name 'a' is not defined
10
10
----------------------------------------------------------------------
0
10
0

Comments

1

If you declare it as a global, you don't have to return it. If you don't use global, you have the function to return a. Without global it be like this:

In [7]: def foo():
   ...:     a = 10
   ...:     return a
   ...: 

In [9]: a = foo()

In [10]: a
Out[10]: 10

With global:

In [11]: a = 0

In [12]: def foo2():
   ....:     global a
   ....:     a = 10
   ....:     

In [13]: a
Out[13]: 0

In [14]: foo2()

In [15]: a
Out[15]: 10

It is you who decide if you want your function to return or to use global. It can simplify you code, it can be useful for function that calculates some constants that you don't want to call it each time. But if use it for single procedure, prefer "return" method.

2 Comments

This does not answer the asked question - What is the use of declaring global in a function instead of declaring variables right at the top.
Whell it is an answer. You can deside if you want your function to return parameter or not to return it (and use global instead)
0

if you are using the same variable inside the loop then there will be a competition between the global and local variable . so if you want to use the value of the global variable in the loop you have to mention the 'global' keyword

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.