On 7/02/2014 6:36pm, Chris Maclachlan wrote:
Hi Mike
None is not the same as int(0) - it's a bit like javascript's NaN
(not-a-number); it's not 0, it's a complete absence of value; an
indeterminate value. Because of this, it can't actually be successfully
cast to a numeric:
>>> a = None
>>> int(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'NoneType'
Python 3's a bit stricter about trying to compare incomparable types
(See e.g.
http://stackoverflow.com/questions/8961005/comparing-none-with-built-in-types-using-arithmetic-operators
for more).
Thanks Chris (and Ben).
I guess that's what they mean by breaking backwards compatibility. I
suppose its for my own good. I'll just rewrite all my code. At least it
will still work in Python 2
But it doesn't all have to be changed because ...
>>> bar = 'x'
>>> foo = None
>>> if not foo:
... bar = 'y'
...
>>> bar
'y'
Mike
The way to correctly compare to None is to use "is", ie:
>>> a = None
>>> b = 1
>>> if a is not b:
... print("woo")
...
woo
Cheers
Chris
On Fri, Feb 7, 2014 at 6:21 PM, Mike Dewhirst <[email protected]
<mailto:[email protected]>> wrote:
It is in Python 2.7 ...
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = None
>>> bar = 2
>>> foo < bar
True
>>>
But it seems not in Python 3.3 ...
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC
v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = None
>>> bar = 2
>>> foo < bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < int()
>>>
Should I rtfm?
Maybe there is a trick to it?
Thanks
Mike
_________________________________________________
melbourne-pug mailing list
[email protected] <mailto:[email protected]>
https://mail.python.org/__mailman/listinfo/melbourne-pug
<https://mail.python.org/mailman/listinfo/melbourne-pug>
_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug
_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug