Python functions modify whatever arguments you pass in,
and the changes are seen outside the function.
Example code:
dct = dict(a='a', b='b')
def modify_dct(thedct):
thedct['a'] = 1
thedct['b'] = 2
if __name__ == '__main__':
print dct
modify_dct(dct)
print dct
print "OK"
Output:
[Dbg]>>>
{'a': 'a', 'b': 'b'}
{'a': 1, 'b': 2}
OK
What just happened?
Unlike C, which has pass by value, whatever you pass to a function other than primitives (int, e.g. 1, 110,)
is actually a reference to that object itself, and not a copy.
So, modifying it inside the function will see it changed outside the function, after the func has run (processed it) on it.
No comments:
Post a Comment