None

Steve Holden sholden at holdenweb.com
Thu Oct 25 01:43:51 CEST 2001


"David Bolen" <db3l at fitlinxx.com> wrote ...
> "Larry Whitley" <ldw at us.ibm.com> writes:
>
> > That's it!  Here's the offending code, a little earlier in process().
> >
> >         elif pc.command == "dual": # command was a dual address cycle
> >             pc.command, None, None = tr.decodeCommand()
> >
> > Looks like it considers None a variable though it is not under the elif
that
> > used it.
>
> Yes, local variable determination is a static compile-time operation,
> based on compiling an assignment, and not runtime based.  But the
> error is runtime based if the variable gets used in the code path
> before being initialized.
>
An attempt to access the value of any local variable before it is assigned
will give a runtime error. As you correctly point out, the compiler
determines which variables are local by a static code analysis: if there is
any binding to the variable in a function it is assumed to be local.

> There's no clean way to ignore components in tuple unpacking, but you
> definitely want to avoid using existing names (such as None), since
> while it's just a name binding to the builtin None object, most code
> works better when "None" really points to that object :-)
>
In strict point of fact, what was actually happening here was that the local
variable None was masking the builtin variable None. When you assign to None
in a module you bind to the module global.

But you can even overwrite the builtin None if you want to, and that
*really* confuses things -- and even more so if it is ever deleted. I had
understood there was special code in the interpreter to reassert None's
value if it was ever deleterd, but this surprised me:

Python 2.1.1 (#2, Sep 26 2001, 09:32:53)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "copyright", "credits" or "license" for more information.
>>> None = "Have a banana"
>>> None
'Have a banana'
>>> print __builtins__.None
None    # still what it always was
>>> del None
>>> print None
None    # back to the builtin
>>> del __builtins__.None
>>> print None
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'None' is not defined
>>> # oh-oh ...

> If you don't want to create a throwaway name, or don't want to keep a
> reference to the other tuple elements around, you could always change
> that line to something like:
>
>     pc.command = tr.decodeCommand()[0]
>
That would certainly work.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list