Assign to True / False in 2.3

Discussion in 'Python' started by Culley Angus, Jul 2, 2003.

  1. Culley Angus

    Culley Angus Guest

    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?.
    Culley Angus, Jul 2, 2003
    #1
    1. Advertising

  2. Culley Angus wrote:

    > 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.
    >>> None = 0

    <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:

    >>> int = float
    >>> file = str
    >>> sys = 'This is really not a module'


    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ Shooters, looters / Now I got a laptop computer
    \__/ Ice Cube
    Erik Max Francis, Jul 2, 2003
    #2
    1. Advertising

  3. Culley Angus

    Just Guest

    In article <>,
    Erik Max Francis <> wrote:

    > Culley Angus wrote:
    >
    > > 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):


    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
    Just, Jul 2, 2003
    #3
  4. Culley Angus

    Max M Guest

    > Culley Angus wrote:

    > 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
    Max M, Jul 2, 2003
    #4
  5. On Wed, Jul 02, 2003 at 03:02:59AM -0700, Erik Max Francis wrote:
    > Culley Angus wrote:
    >
    > > 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.
    > >>> None = 0

    > <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.
    Andrew Bennetts, Jul 2, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    652
    Chris Theis
    Jun 18, 2004
  2. Pierre Quentel

    "0 in [True,False]" returns True

    Pierre Quentel, Dec 12, 2005, in forum: Python
    Replies:
    59
    Views:
    1,020
    Grant Edwards
    Dec 16, 2005
  3. André
    Replies:
    3
    Views:
    1,562
  4. bdb112
    Replies:
    45
    Views:
    1,320
    jazbees
    Apr 29, 2009
  5. Shea Martin

    false or true == true .... WTF?

    Shea Martin, Apr 5, 2007, in forum: Ruby
    Replies:
    4
    Views:
    97
    Bertram Scharpf
    Apr 5, 2007
Loading...

Share This Page