ARGUMENT NUANCES
SETUP • In Python function invocations, copies of the values of arguments are used to initialize the parameters. This style of parameter passing is known as pass by value . • Because copies are used, the values of the arguments cannot be modified within the function being invoked.
PYTHON FILES Program Program cautionary.py cautionary.py Module Module tale.py tale.py import tale def f( x, y ) : rmbr = x city1 = 'London' x = y city2 = 'Paris’ y = rmbr tale.f( city1, city2 ) print( city1, city2 )
PYTHON FILES Program Program cautionary.py cautionary.py Module Module tale.py tale.py import tale def f( x, y ) : rmbr = x city1 = 'London' x = y city2 = 'Paris’ y = rmbr tale.f tale.f( city1, city2 ) ( city1, city2 ) print( city1, city2 ) main main city1 ’London' city2 'Paris'
PYTHON FILES Program Program cautionary.py cautionary.py Module tale.py Module tale.py import tale def f( x, y ) : def f( x, y ) : rmbr = x city1 = 'London' x = y city2 = 'Paris’ y = rmbr tale.f( city1, city2 ) tale.f ( city1, city2 ) print( city1, city2 ) f() f() main main city1 ’London' rmbr x city2 'Paris' y
PYTHON FILES Program Program cautionary.py cautionary.py Module tale.py Module tale.py import tale def f( x, y ) : rmbr = x rmbr = x city1 = 'London' x = y city2 = 'Paris’ y = rmbr tale.f( city1, city2 ) tale.f ( city1, city2 ) print( city1, city2 ) f() f() main main city1 ’London' rmbr x city2 'Paris' y
PYTHON FILES Program Program cautionary.py cautionary.py Module tale.py Module tale.py import tale def f( x, y ) : rmbr = x rmbr = x city1 = 'London' x = y city2 = 'Paris’ y = rmbr tale.f( city1, city2 ) tale.f ( city1, city2 ) print( city1, city2 ) f() f() main main city1 ’London' rmbr x city2 'Paris' y
PYTHON FILES Program Program cautionary.py cautionary.py Module tale.py Module tale.py import tale def f( x, y ) : rmbr = x city1 = 'London' x = y x = y city2 = 'Paris’ y = rmbr tale.f( city1, city2 ) tale.f ( city1, city2 ) print( city1, city2 ) f() f() main main city1 ’London' rmbr x city2 'Paris' y
PYTHON FILES Program cautionary.py Program cautionary.py Module tale.py Module tale.py import tale def f( x, y ) : rmbr = x city1 = 'London' x = y x = y city2 = 'Paris’ y = rmbr tale.f tale.f( city1, city2 ) ( city1, city2 ) print( city1, city2 ) f() f() main main city1 ’London' rmbr x city2 'Paris' y
PYTHON FILES Program Program cautionary.py cautionary.py Module tale.py Module tale.py import tale def f( x, y ) : rmbr = x city1 = 'London' x = y city2 = 'Paris’ y = rmbr y = rmbr tale.f( city1, city2 ) tale.f ( city1, city2 ) print( city1, city2 ) f() f() main main city1 ’London' rmbr x city2 'Paris' y
PYTHON FILES Program Program cautionary.py cautionary.py Module tale.py Module tale.py import tale def f( x, y ) : rmbr = x city1 = 'London' x = y city2 = 'Paris’ y = rmbr y = rmbr tale.f( city1, city2 ) tale.f ( city1, city2 ) print( city1, city2 ) f() f() main main city1 ’London' rmbr x city2 'Paris' y
PYTHON FILES Program Program cautionary.py cautionary.py Module Module tale.py tale.py import tale def f( x, y ) : rmbr = x city1 = 'London' x = y city2 = 'Paris’ y = rmbr tale.f( city1, city2 ) tale.f ( city1, city2 ) print( city1, city2 ) f() f() main main main main city1 city1 ’London' rmbr x city2 city2 'Paris' y
PYTHON FILES Program Program wipe.py wipe.py Module Module zero.py zero.py import zero def zero( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ i ] = 0 zero.out( nbrs ) print( nbrs )
PYTHON FILES Program Program wipe.py wipe.py Module Module zero.py zero.py import zero def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ i ] = 0 zero.out( nbrs ) zero.out ( nbrs ) print( nbrs ) main main nbrs 3 1 4
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ i ] = 0 zero.out( nbrs ) zero.out ( nbrs ) print( nbrs ) out() out() main main nbrs x 3 1 4 i n
PYTHON FILES Program wipe.py Program wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 3 1 4 i n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = n = len len( x ) ( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ i ] = 0 zero.out( nbrs ) zero.out ( nbrs ) print( nbrs ) out() out() main main nbrs x 3 1 4 i n
PYTHON FILES Program Program wipe.py wipe.py Module Module zero.py zero.py import zero def out( x) : def out( x) : n = len n = len( x ) ( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 3 1 4 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module Module zero.py zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for for i in range( 0, n ) : in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 3 1 4 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for for i in range( 0, n ) : in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 3 1 4 0 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ x[ i ] = 0 ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 3 1 4 0 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ x[ i ] = 0 ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 1 4 0 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for for i in range( 0, n ) : in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 1 4 0 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for for i in range( 0, n ) : in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 1 4 1 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ x[ i ] = 0 ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 1 4 1 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ x[ i ] = 0 ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 0 4 1 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for for i in range( 0, n ) : in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 0 4 1 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for for i in range( 0, n ) : in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 0 4 2 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ x[ i ] = 0 ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 0 4 2 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for i in range( 0, n ) : x[ x[ i ] = 0 ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 0 0 2 i 3 n
PYTHON FILES Program Program wipe.py wipe.py Module zero.py Module zero.py import zero def out( x) : def out( x) : n = len( x ) nbrs = [ 3, 1, 4 ] for for i in range( 0, n ) : in range( 0, n ) : x[ i ] = 0 zero.out zero.out( nbrs ) ( nbrs ) print( nbrs ) out() out() main main nbrs x 0 0 0 2 i 3 n
Recommend
More recommend