Tautologies are overrated

$ python2.4</p>

<blockquote><blockquote><blockquote><p>True,False = False, True
a = True
if a:
...     print 'Hello World'
if not a:
...     print 'Hello World'
Hello World

I am a big-time Python fan but this is crazy!

(discovered via python-list)



  1. A similar problem exists for keywords:

    >>> abs(-1)
    >>> abs=1
    >>> abs(-1)
    Traceback (most recent call last):
    File “”, line 1, in ?
    TypeError: ‘int’ object is not callable

    Contrast with Ruby

    irb(main):001:0> -1.abs
    => 1
    irb(main):002:0> -1.abs =3
    NoMethodError: undefined method `abs=’ for -1:Fixnum
    from (irb):2

  2. I tried this in C++: true = false
    And the compiler says, “error: non-lvalue in assignment”
    I wonder how that works in Python.

  3. Wow. Why can you do that? Assign values to False and True?

  4. This is not crazy. You have assigned the ‘False’ bool type to ‘a’, and you get what you expected. :-)

    Actually, python must deny the use of True and False as l-values.

  5. John: Thanks for pointing that out. Looks like there are still many warts in Python.

  6. Pramod: C++ has true and false as special keywords from the beginning. Python just added it (relatively) recently and apparently things like these are still possible.

  7. Aggelos: Yes, that actually works :| , I guess they have `True` and `False` as just instances of the `bool` class and hence things like these are possible i.e. they are not keywords, they are just objects which can be manipulated like any other.

  8. Sridhar: I know what the program is doing. I expected it not to allow assignment to `True` and `False`, that is what I am surprised about.

  9. True and False are not constants in Python. In fact they are the only possible instances of the new ‘bool’ type.

    >>> c = bool(5)
    >>> c
    >>> c = bool(0)
    >>> c

    However Python should be setting these names as immutable, similar to the None keyword in 2.4

  10. This should not happen. may be u can suggest python guys not to allow True or False as lvalue.

  11. Anything that is not a keyword can be overrided, and there are very few keywords on purpose! It’s not a wart, it’s a design decision. Live with it :)

    Python is for consenting adults.

  12. This has a lot of potential for obfuscated stuff. :)

  13. john compared ruby properties with python names
    Not a good comparision

    I think this is closer to what john demonstrated in ruby
    using python properties

    >>>class absInt(int):
    … abs = property(fget=lambda self: abs(self))
    >>> i = absInt(-1)
    >>> i.abs
    >>> i.abs = 5
    Traceback (most recent call last):
    File “”, line 1, in ?
    AttributeError: can’t set attribute

    I think i like the idea of limiting the number of keywords the way python does.

Your comments?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s