Computer Science Canada

[python] Something neat

Author:  Raknarg [ Wed Jan 15, 2014 3:43 pm ]
Post subject:  [python] Something neat

This didn't seem to fit anywhere in the python section, but apparently this is valid:

True = False

Now True will equate to False.

Author:  Tony [ Wed Jan 15, 2014 4:09 pm ]
Post subject:  RE:[python] Something neat

although
code:

>>> True = False
>>> print True
False
>>> print 1==1
True

Author:  Raknarg [ Wed Jan 15, 2014 6:26 pm ]
Post subject:  RE:[python] Something neat

How strange. Any idea how this is?

Author:  Tony [ Wed Jan 15, 2014 7:15 pm ]
Post subject:  RE:[python] Something neat

there's a difference between "True" the variable and "True" the value. It kind of looks like there's a variable called True and it initially holds a value that has a string representation of "True".

Author:  Raknarg [ Wed Jan 15, 2014 7:16 pm ]
Post subject:  RE:[python] Something neat

Right, I was thinking something along that, maybe it was a built in function that returns true

Author:  Dreadnought [ Wed Jan 15, 2014 7:34 pm ]
Post subject:  Re: [python] Something neat

Raknarg wrote:

Right, I was thinking something along that, maybe it was a built in function that returns true

Not a function, its an object of type bool.The object has a value of True in expressions but as Tony says it has a string representation (what you see when you print it) of 'True'.
You can see this
Python:
>>> int(True)
1
>>> type(True)
<type 'bool'>
>>> type(True.__repr__())
<type 'str'>
>>> True.__repr__()
'True'


EDIT: Interesting note, True = False is illegal in python 3 (at least in my version). It seems that they are now keywords.

Author:  Raknarg [ Wed Jan 15, 2014 7:43 pm ]
Post subject:  RE:[python] Something neat

Ah. Thanks for that.


: