True/False
Ian Bicking
ianb at colorstudy.com
Tue Apr 22 20:00:08 EDT 2003
On Tue, 2003-04-22 at 16:17, Erik Max Francis wrote:
> If done, then a blanket statement like yours above would start
> generating warnings eventually; they'd be harmless warnings, but they'd
> be unnerving and annoying.
>
> > import sys
> > if sys.version_info < (2, 3):
> > True, False = 1==1, 0==1
>
> This would be okay (although the untyped version of the True and False
> builtins were introduced in 2.2, not 2.3). The solution I've adopted
> is:
>
> try:
> True
> except NameError:
> True, False = 1, 0
Not safe at all:
Python 2.3a2+ (#2, Mar 21 2003, 22:13:05)
[GCC 3.2.3 20030316 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
... None
... except NameError:
... None = 0
...
<stdin>:4: SyntaxWarning: assignment to None
Syntax-related errors/warnings aren't runtime. Though strangely:
>>> if 0:
... None = 0
...
>>> if False:
... None = 0
...
<stdin>:2: SyntaxWarning: assignment to None
Maybe there's an optimization going on in this case, though (and to do
the optimization properly in both cases you'd have to catch assignment
to False too!)
Ian
More information about the Python-list
mailing list