From: Carey Tilden on
On Thu, Aug 5, 2010 at 8:42 AM, wheres pythonmonks
<wherespythonmonks(a)gmail.com> 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?

In addition to all the other fine responses, you also might want to
take a look at the python grammar [1]. The relevant line is:

comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'

Cheers,
Carey

[1] http://docs.python.org/reference/grammar.html
From: Chris Rebert on
On Thu, Aug 5, 2010 at 8:56 AM, Roald de Vries <downaold(a)gmail.com> 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'.

Absolutely incorrect. Read the final paragraph of
http://docs.python.org/reference/expressions.html#notin

Cheers,
Chris
--
http://blog.rebertia.com
From: Chris Rebert on
On Thu, Aug 5, 2010 at 8:42 AM, wheres pythonmonks
<wherespythonmonks(a)gmail.com> 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?

It's not quite the same type of sugar as that, but people sometimes
forget that comparisons can be chained:

if x < y <= z:

Same as:

if x < y and y <= z:
# but y is only evaluated once

Cheers,
Chris
From: Roald de Vries on
On Aug 5, 2010, at 6:11 PM, Chris Rebert wrote:
> On Thu, Aug 5, 2010 at 8:56 AM, Roald de Vries <downaold(a)gmail.com>
> 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'.
>
> Absolutely incorrect. Read the final paragraph of
> http://docs.python.org/reference/expressions.html#notin

Oops, sorry :$.

From: Dave Angel on
Roald de Vries wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">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'.
> 'not x is None' is the same as 'not (x is None)'
>
> Cheers, Roald
>
Did you try it?

Python 2.6.4 (r264:75706, Jan 22 2010, 16:41:54) [MSC v.1500 32 bit
win32
Type "help", "copyright", "credits" or "license" for more informatio
>>> 4 is not None
True
>>> True is not None
True
>>> False is not None
True
>>> None is not None
False
>>>

Looks to me like
x is not None

is equivalent to
not (x is None)

(I get same results for 3.1)

DaveA