Declaring A Function Argument As Global?

Jp Calderone exarkun at intarweb.us
Thu Jan 16 15:16:47 EST 2003


On Thu, Jan 16, 2003 at 07:30:07PM +0000, Tim Daneliuk wrote:
> 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...

  It might be helpful to look at it in terms of the special methods that
this is equivalent to.

  list[a:b] = x

is the same as

  list.__setslice__(a, b, x)

or (because __setslice__ is deprecated) in 2.3:

  list.__setitem__(slice(a, b), x)


  Hopefully it is obvious from this example that no names are being rebound,
the list is just being mutated in place.

  Jp

-- 
There are 10 kinds of people: those who understand binary and those who do
not.
--
 12:00am up 31 days, 9:48, 2 users, load average: 0.07, 0.09, 0.04
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030116/0f25cacb/attachment.sig>


More information about the Python-list mailing list