Custom Search

Sunday, January 3, 2010

Python function with global and local variables

Python function with global and local variables

Global and Local variable scope inside and outside the python functions

==========================================

balance = 1000

def deposit(amount):

global balance

balance = balance + amount

def withdraw(amount):

global balance

balance = balance - amount

We first create a variable called balance initialized with the value 1000 (say we have 1000 Rupees

balance initially). Then we define two simple functions deposit and withdraw. The interesting

things about these functions are:

• Both functions do not use return

• There is a new keyword in the body of both functions - global

The line:

global balance

simply tells Python that the variable balance being used in the next line (and all the other lines of

the function, if any) refers to the variable balance declared outside the function (such variables are

called global variables in programming language terminology).

The two functions above (deposit and withdraw) are said to have a side effect - in this case, the side

effect is altering the value of a global variable.

==========================================

The execution of a function introduces a new symbol table used for the local variables of the function. More precisely,

all variable assignments in a function store the value in the local symbol table; whereas variable references (search) first

look in the local symbol table, then in the global symbol table, and then in the table of built-in names. Thus, global variables

cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is

called; thus, arguments are passed using call by value (where the value is always an object reference (address), not the value

of the object).4.1 When a function calls another function, a new local symbol table is created for that call.

========================================== Example of python function

balance = 1000

k=100

def deposit(amount):

global balance

balance = balance + amount

# Here 'k' is a local variable to function,so we can assign value to it and modify.

k=50

# Printing value of local variable 'k'.

print 'Local k : ',k

def withdraw(amount):

global balance

balance = balance - amount

# Accessing global variable 'k' that is not local to this function.That is we can only access it value,not modify.

print 'Accessing global k : ',k+30

#But we can assign value to global variable 'balance', because 'balance' is declared as global variable inside the function.

#k = 20 <---- Error, uncommenting this line cause error on above line print 'Accessing global k : ',k+30

print "Balance : ",balance

deposit(100)

print " Balance After Deposit : ",balance

withdraw(200)

print " Balance After Withdraw : ",balance

print 'Value of k : ',k

OUTPUT

-------------

Balance : 1000

Local K : 50

Balance After Deposit : 1100

Accessing global k : 130

Balance After Withdraw : 900

Value of k : 100

==========================================

No comments:

Post a Comment