In the function, we can read the value of global variable A
However, if you want to modify global variable A, you must using following methods:
global:
use global to modify global variable
ex:
x=1
def test():
global x
x=3
access attribute in other module:
using import
ex:
import test
test.var = 2
nested function:
ex:
def test:
x=3
def test2:
print x
test2 can access x defined in test
keyword parameter
ex:
def test(a, b):
print a, b
test( a=3, b=2)
default parameter:
ex:
def test(a, b=3):
printf a, b
test( 2)
arbitrary argument:
ex:
def test(*args):
print args
test( 1,2,3)
** is for keyboard argument
ex:
def test(**args):
print args
test(a=1,b=2)
use * in caller:
unpack a collection of arguments
ex:
a= (1,2,3,4)
test( *a)
use ** in caller:
unpack a dictionary of key/value pairs
沒有留言:
張貼留言