Is it really good?
Bengt Richter
bokr at oz.net
Tue Jan 7 19:39:22 EST 2003
On Tue, 07 Jan 2003 11:12:47 +0200, Nadav Horesh <NadavH at envision.co.il> wrote:
>Can someone explain why python does not raise NameError in the first
>test line blow.
>I get similar results with Python 2.3a1, wich (obviously) answer False
>instead of 0.
>
>===============================================================
>Python 2.2.2 (#5, Dec 19 2002, 10:39:16)
>[GCC 3.2.1] on linux2
>Type "copyright", "credits" or "license" for more information.
>IDLE 0.8 -- press F1 for help
>
> >>> 2 == 3 is good
>0 # ????????????????????????
>
> >>> (2 == 3) is good
>Traceback (most recent call last):
> File "<pyshell#1>", line 1, in ?
> (2 == 3) is good
>NameError: name 'good' is not defined
> >>> 2 == (3 is good)
>Traceback (most recent call last):
> File "<pyshell#2>", line 1, in ?
> 2 == (3 is good)
>NameError: name 'good' is not defined
> >>>
>
>===============================================================
[16:38] C:\pywk\clp>ppcomp "2==3 is good"
Module(
None,
Stmt(
[
Discard(
Compare(
Const(
| 2),
[
(
| '==',
| Const(
| 3)),
(
'is',
Name(
'good'))]))]))
I see what is happening, I think. It is treating 'is' as just
one more compare operator in a chain, and 2==3 short-cuts to false,
so the next thing doesn't get evaluated. E.g.,
>>> 2==3 is good
0
>>> 2<=3 is good
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'good' is not defined
>>> 2<=3 is 4 is good
0
>>> 2<=3 is 3 is good
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'good' is not defined
Regards,
Bengt Richter
More information about the Python-list
mailing list