Re: [Tutor] pychecker: x is None or x == None

2005-08-13 Thread Alan G
>if x == None:
>do_something()
> 
> but then someone thought that we should really change these to
> 
>if x is None:
>do_something()
> 
> However. if you run pychecker on these two snippets of code, it
> complains about the second, and not the first:

Personally I'd use

if not x:
  do_something()

No idea what pyChecker thinks of that though! :-)
And of course it's slightly different to a specific check for None, 
it will catch 0, [], '',"", etc...

Of the two forms you suggest I'd stick with equality since the 
identity test (is) assumes that only one instance of None ever 
exists which could potentially change in some future exotic 
version of Python... You really are interested in the value 
of x not its identity.

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pychecker: x is None or x == None

2005-08-12 Thread Kent Johnson
Duncan Gibson wrote:
> We've been programming in Python for about a year. Initially we had a
> lot of tests of the form
> 
> if x == None:
> do_something()
> 
> but then someone thought that we should really change these to
> 
> if x is None:
> do_something()
> 
> However. if you run pychecker on these two snippets of code, it
> complains about the second, and not the first:
> 
> x.py:6: Using is None, may not always work
> 
> So the question is, which one should we really be using?
> If it is the second, how do I get pychecker to shut up?

Searching comp.lang.python for 'pychecker "is None"' finds this discussion:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/a289d565a40fa435/9afaeb22763aadff?q=pychecker+%22is+None%22&rnum=1&hl=en#9afaeb22763aadff

which says that pychecker is confused by the comparison to a constant and you 
should ignore it.

There is a pychecker test (test_input\test90.py and test_output\test90) which 
shows pychecker ignoring 'is not None' so I think this is a pychecker bug in 
Python 2.4. It is in the bug tracker here:
https://sourceforge.net/tracker/?group_id=24686&atid=382217&func=detail&aid=1227538

The bug references PEP 290 which clearly says that 'is None' is the preferred 
test:
http://www.python.org/peps/pep-0290.html#testing-for-none

I don't see a way to turn off this test but I haven't looked in detail at the 
config options.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pychecker: x is None or x == None

2005-08-12 Thread Duncan Gibson

We've been programming in Python for about a year. Initially we had a
lot of tests of the form

if x == None:
do_something()

but then someone thought that we should really change these to

if x is None:
do_something()

However. if you run pychecker on these two snippets of code, it
complains about the second, and not the first:

x.py:6: Using is None, may not always work

So the question is, which one should we really be using?
If it is the second, how do I get pychecker to shut up?

I've hunted around in the documentation, and if there is a clear
discussion about this issue, I must have missed it.

Cheers
Duncan
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor