None weirdness
Ian McConnell
ian at infosar.co.uk
Mon Dec 2 07:42:30 EST 2002
Can someone explain what is going on here?
#!/usr/bin/python2.2
def main():
a = None
None = 1
if __name__ == "__main__":
main()
Traceback (most recent call last):
File "sub.py", line 8, in ?
main()
File "sub.py", line 4, in main
a = None
UnboundLocalError: local variable 'None' referenced before assignment
Exit 1
Python seems to be doing the 'None=1' assignment before 'a=None'!
If this seems like pretty weird code, then it resulted from me trying to do
a, None = func()
func() returns a pair of answers, but I was only interested in the first, so
I thought that "a, None" would just drop the second value. However, it
redefined None.
I now use
a, = func()
which works, but doesn't seem as clear to me.
More information about the Python-list
mailing list