Is it really good?

Grant Edwards grante at visi.com
Wed Jan 8 03:34:12 CET 2003


In article <mailman.1041983572.25078.python-list at python.org>, Skip Montanaro wrote:
> 
>     Nadav> Can someone explain why python does not raise NameError in the
>     Nadav> first test line b[e]low.
> 
>     >>> 2 == 3 is good
>     0     # ????????????????????????
> 
> Chained operations.  The above expression is effectively
> 
>     (2 == 3) and (3 is good)
>
> The first is false, so the second is never evaluated.

Why do you say that?

That logic would seem to indicate that
 
  0 is 0
  
should false, since the first term is false, the "is" short-circuits and
returns false. It doesn't:

  Python 1.5.2 (#1, Apr  3 2002, 18:16:26)  [GCC 2.96 20000731 (Red Hat Linux 7.2 2 on linux-i386
  Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
  >>> 0 is 0
  1

Your statement also means that

  >>> (2==3) is (2==3)
  1
  
should return false, but it doesn't.



So why does

 (2==3) is 0
 
return false? 

Apparently the expression (2==3) returns a false value that prints as 0, but
it's not the same 0 as the one on the right hand side.

One might guess that (2==3) returns some sort of temporary false value that
can never "be" anything else since it isn't a "real" object.  If that's the
case, then why does the 1 returned by (2!=3) act similarly in this case:

  >>> (2!=3) is 1
  0

But not in this case:

  >>> (2!=3) is good
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  NameError: name 'good' is not defined
  >>> 

The value returned by (2!=3) apparently can't "be" anything else either, but
"is" still evaluates the RHS.  Apparently, there is a "true" value that
prints as "1", but isn't "1":

>>> (2!=3) is (2!=3)
1

All this still doesn't explain why

 (2==3) is good
 
doesn't evaluate the right hand side of the "is".... 

-- 
Grant Edwards                   grante             Yow!  I HAVE a towel.
                                  at               
                               visi.com            




More information about the Python-list mailing list