Assign to True / False in 2.3

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Culley Angus

    Assign to True / False in 2.3

    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?.

  • Andrew Bennetts

    #2
    Re: Assign to True / False in 2.3

    On Wed, Jul 02, 2003 at 03:02:59AM -0700, Erik Max Francis wrote:[color=blue]
    > Culley Angus wrote:
    >[color=green]
    > > 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.[/color]
    >
    > 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.[color=green][color=darkred]
    > >>> None = 0[/color][/color]
    > <stdin>:1: SyntaxWarning: assignment to None[/color]

    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.


    Comment

    • Erik Max Francis

      #3
      Re: Assign to True / False in 2.3

      Culley Angus wrote:
      [color=blue]
      > 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.[/color]

      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.[color=blue][color=green][color=darkred]
      >>> None = 0[/color][/color][/color]
      <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:
      [color=blue][color=green][color=darkred]
      >>> int = float
      >>> file = str
      >>> sys = 'This is really not a module'[/color][/color][/color]

      --
      Erik Max Francis && [email protected] && 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

      Comment

      • Just

        #4
        Re: Assign to True / False in 2.3

        In article <3F02ADD3.6047F [email protected] >,
        Erik Max Francis <[email protected] m> wrote:
        [color=blue]
        > Culley Angus wrote:
        >[color=green]
        > > 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.[/color]
        >
        > 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):[/color]

        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

        Comment

        • Max M

          #5
          Re: Assign to True / False in 2.3

          > Culley Angus wrote:
          [color=blue]
          > 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)[/color]


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

          regards Max M

          Comment

          • Andrew Bennetts

            #6
            Re: Assign to True / False in 2.3

            On Wed, Jul 02, 2003 at 03:02:59AM -0700, Erik Max Francis wrote:[color=blue]
            > Culley Angus wrote:
            >[color=green]
            > > 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.[/color]
            >
            > 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.[color=green][color=darkred]
            > >>> None = 0[/color][/color]
            > <stdin>:1: SyntaxWarning: assignment to None[/color]

            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.


            Comment

            Working...