Assign to True / False in 2.3

C

Culley Angus

Just downloaded the latest beta of 2.3, and having a bit of fun playing
with the new goodies, lovely work by the way :)

I was a little suprised to find that I could assign a value to 'True',
and 'False' without warning though, and was wondering if this is deliberate.

For example:
if (1 == True): print "true"
True = 0
if (1 == True): print "true"
else: print "true is false"

This snippet is fairly unlikely to ever be written by a sober
individual, but if something similar is constructed by accident, the
repercussions may be interesting if not detected.

Can True (or False for that matter) be relied on for this sort of direct
comparison?.
 
E

Erik Max Francis

Culley said:
I was a little suprised to find that I could assign a value to 'True',
and 'False' without warning though, and was wondering if this is
deliberate.

This is true of pretty much all Python features. The only special
dispensation goes to None, which is a warning now (in the 2.3 beta):

Python 2.3b2 (#1, Jun 29 2003, 20:30:58)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.<stdin>:1: SyntaxWarning: assignment to None

Python takes the approach of "We're all adults here." If you're using
Python, it presumes that you don't need handholding, and that you won't
do things you know you shouldn't do. For instance, imagine the havoc
that things like this would cause:
 
J

Just

Erik Max Francis said:
This is true of pretty much all Python features. The only special
dispensation goes to None, which is a warning now (in the 2.3 beta):

Actually, at some point there was a warning for True and False as well,
but it was taken out because there is plenty of code out there like this:

try:
True, False
except NameError:
True, False = 1, 0

(I'm not entirely sure, it can also be that there never was a warning in
place, but it was discussed.)

Just
 
M

Max M

Culley said:
This is true of pretty much all Python features. The only special
dispensation goes to None, which is a warning now (in the 2.3 beta)


I most often use this freedom to overwrite "id". I Guess that it's a bad
Zope habbit...

regards Max M
 
A

Andrew Bennetts

Culley said:
I was a little suprised to find that I could assign a value to 'True',
and 'False' without warning though, and was wondering if this is
deliberate.

This is true of pretty much all Python features. The only special
dispensation goes to None, which is a warning now (in the 2.3 beta):

Python 2.3b2 (#1, Jun 29 2003, 20:30:58)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.<stdin>:1: SyntaxWarning: assignment to None

Incidentally, the reason why a SyntaxWarning isn't raised for True and False
is that there is a lot of existing code for older pythons that do tricks
like:

True = (1 == 1)
False = not True

Or something similar, and the Python team didn't want to break code, or
cause spurious warnings for code that might otherwise work perfectly well
with 2.3 and 2.2 or even 1.5.2 (depending on what else it did, of course).

-Andrew.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top