Python Variables that are defined inside a function body have a local scope, and those defined outside have a global scope and variable.
That indicate local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions. When we call a function, the variables declared inside it are brought into function. For example you can see below code.
#!/shiva/bin/python
total = 0; # This is global variable.
# Function definition is here
def sum( t1, t2 ):
# Add both the parameters and return them."
total = t1 + t2; # Here total is local variable.
print "Inside the function local total : ", total
return total;
# Now you can call sum function
sum( 6, 5 );
print "Outside the function global total : ", total
Output:
Inside the function local total :11
Outside the function global total : 0
0 Comment(s)