2011-08-20

Python Constant Madness

Somehow Python does and does not have constants. The following examples, which can be tried out on the interactive interpreter, are of not much practical use, but interesting however:

Type "help", "copyright", "credits" or "license" for more information.

>>> temp_true = True

>>> temp_false = False

>>> True = temp_false

>>> False = temp_true

>>> True

False

>>> False

True


Even the built-in constants True and False can be overwritten with arbitrary content. This flexibility is probably rarely required and if this happens by accident, the logic of the following program is most likely screwed.

>>> True = "hello world"

>>> True

'hello world' 
Ruby does have a little advantage here, since it does not allow assigning new values to true and false.

No comments:

Post a Comment