From: Ethan Furman on 5 Aug 2010 13:09 Roald de Vries wrote: > On Aug 5, 2010, at 5:42 PM, wheres pythonmonks wrote: >> How does "x is not None" make any sense? "not x is None" does make >> sense. >> >> I can only surmise that in this context (preceding is) "not" is not a >> unary right-associative operator, therefore: >> >> x is not None === IS_NOTEQ(X, None) >> >> Beside "not in" which seems to work similarly, is there other >> syntactical sugar like this that I should be aware of? > > 'not None' first casts None to a bool, and then applies 'not', so 'x is > not None' means 'x is True'. This is not correct. 'is not' is a single operator, and in this case is checking that the object 'x' is not the same as the object 'None'. No boolean conversions are done -- False and True have nothing to do with this example. Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. --> x = 5 --> x is not None True --> x = True --> x is not None True --> x = ('this','is','a','tuple') --> x is not None True --> x = None --> x is not None False ~Ethan~
From: Benjamin Kaplan on 5 Aug 2010 18:44 On Thu, Aug 5, 2010 at 9:07 AM, wheres pythonmonks <wherespythonmonks(a)gmail.com> wrote: > Well, I am not convinced of the equivalence of not None and true: > >>>> not None > True >>>> 3 is True; > False >>>> 3 is not None > True >>>> > > P.S. Sorry for the top-post -- is there a way to not do top posts from > gmail? I haven't used usenet since tin. > Scroll down in the email and press enter a few times.
From: Rhodri James on 5 Aug 2010 19:15 On Thu, 05 Aug 2010 17:07:53 +0100, wheres pythonmonks <wherespythonmonks(a)gmail.com> wrote: > Well, I am not convinced of the equivalence of not None and true: > >>>> not None > True >>>> 3 is True; > False >>>> 3 is not None > True >>>> You're not testing for equivalence there, you're testing for identity. "is" and "is not" test whether the two objects concerned are (or are not) the same object. Two objects can have the same value, but be different objects. The interpreter can fool you by caching and reusing objects which have the same value when it happens to know about it, in particular for small integers, but this is just a happy accident of the implementation and in no way guaranteed by the language. For example: >>> "spam, eggs, chips and spam" is "spam, eggs, chips and spam" True >>> a = "spam, eggs, chips and spam" >>> b = "spam, eggs, chips and spam" >>> a is b False >>> a == b True Also, remember that "is not" is a single operator, *not* the concatenation of "is" and "not". Your last test is probably not checking what you think it is :-) >>> 3 is (not None) False -- Rhodri James *-* Wildebeest Herder to the Masses
From: Steven D'Aprano on 5 Aug 2010 21:26 On Thu, 05 Aug 2010 12:07:53 -0400, wheres pythonmonks wrote: > P.S. Sorry for the top-post -- is there a way to not do top posts from > gmail? I haven't used usenet since tin. Er, surely you can just move the cursor before you start typing??? -- Steven
From: Bruno Desthuilliers on 6 Aug 2010 03:25
Roald de Vries a �crit : > > 'not None' first casts None to a bool, and then applies 'not', so 'x is > not None' means 'x is True'. Obviously plain wrong : Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 42 is not None True >>> 42 is True False >>> Please check your facts before posting !-) |