Movatterモバイル変換


[0]ホーム

URL:


Newbie Q: var params

Andrew Dalkedalke at acm.org
Tue Apr 10 23:59:10 EDT 2001


zzzzz wrote:>It looks like "parameter=[2,4,6,8]" creates a new local variable that>overrides the passed parameter. How do I do this properly so that the>passed parameter changes in all parameter type cases?You don't have the right internal model for how Python passesinformation about.  Everything is "by reference" so whenyou say:>>> def paramtest(parameter):you are creating a new local variable which is a reference tothe data passed in to it.Later on when you say   parameter=[2,4,6,8]you use the same local variable, but this time it's a referenceto a new object.To do what you want it to do you would have to tell theoriginal object (passed in by reference) to update itself withnew data.  That will differ depending on the object - and someobjects are immutable, meaning they cannot be changed.For lists this is done as   parameter[:] = [2,4,6,8]which says to replace the complete list ([:] means "starting fromthe beginning and going to the end") with the values in [2,4,6,8].                    Andrewdalke at acm.org


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp