Declaring A Function Argument As Global?

John Roth johnroth at ameritech.net
Thu Jan 16 17:34:09 EST 2003


"Tim Daneliuk" <tundra at tundraware.com> wrote in message
news:8r070b.sfc2.ln at boundary.tundraware.com...
> Skip Montanaro wrote:
> >     >> def lhandler(list):
> >     >>     list[:] = list[1:]
> >
> >     Tim> 'Works like a charm.  But why?
> >
> > list[:] on the left-hand side of an assignment assigns to the entire
list.
> > It's effectively
> >
> >     list[0:len(list)] = rhs
>
> The slicing part I understand well.  What I do not grasp is why using
> this construct on the lhs gives you access to the actual list
> in question, but list = list[1:] refers to the local variable (formal
> parameter) 'list'.  It is the semantics of scope that is confusing me
> here, not the list operation...

If you think about it, it has to work that way, or you couldn't
ever assign to a position in a list, or an element of a dictionary.

Effectively, all of the following work the same way:

list[x] = zzz
list[:] = yyy
dict{'foobar'} = "snafu"

They access part of a mutable object. If you couldn't do that,
you'd have to use some kind of functional notation to do those
assignments. That may be a good idea in a functional language,
but Python is not basically a functional language.

Another way of thinking about it is that assignment knows
the difference between a variable and an object. It binds the rhs
to the former, and passes a message to the latter telling it to
update itself.

John Roth

>
> Thanks,
> ----------------------------------------------------------------------
--------
> Tim Daneliuk
> tundra at tundraware.com
>






More information about the Python-list mailing list