Python reference model -
i have hard time understand python reference model
def changer(a,b): = 2 b[0] = 'spam' x = 1 l = [1,2] changer(x,l) x,l (1,['spam',2])
here comes question, assignment b[0] = 'spam' : want know how python modify mutable object in way(instead of create new string objects , link variable b object not impact original object pointed l)
thanks
a
, b
both references objects.
when reassign a
alter object a
referencing.
when reassign b[0]
reassigning reference contained within b
. b
still references same list object did originally, list passed changer
function.
python
No comments:
Post a Comment