l=l (was Re: list.sort())

Alex Martelli aleaxit at yahoo.com
Mon Jun 18 03:25:56 EDT 2001


"Nick Perkins" <nperkins7 at home.com> wrote in message
news:Xr7X6.265080$eK2.55163158 at news4.rdc1.on.home.com...
>
> "Rikard Bosnjakovic" <bos at hack.org> wrote in message
> ...
> > >>> l = l
> ...
> I am pretty sure that
> l = l
> will never have any effect, whatsoever.

Not in current Python, EXCEPT that it makes l local, which may
cause an error in certain cases.  E.g.:

D:\py21>python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
Alternative ReadLine 1.4 -- Copyright 2001, Chris Gonnerman
>>> l=list('ciao')
>>> def f():
...   print '-'.join(l)
...
>>> f()
c-i-a-o
>>> def g():
...   l=l
...   print '-'.join(l)
...
>>> g()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in g
UnboundLocalError: local variable 'l' referenced before assignment
>>>

As you can see, the ONLY difference between functions f() and g()
is that the latter does have a line:
    l=l
This tells the compiler that l is local in g() [while it's global
in f(), since f() does NOT rebind l], whence the error -- it can of
course be fixed by adding to g() a further line:
    global l


Alex






More information about the Python-list mailing list