default arguments newbie question

Erik Max Francis max at alcyone.com
Wed Jan 10 16:37:17 EST 2001


Aahz Maruch wrote:

> Yup.  Technically, you really want to write "if l is None", though; if
> you don't actually care about it being precisely None, the common
> idiom
> is to write "if l".

Eh?  I don't see that.  Any value which is not None will compare unequal
to None, correct?  Certainly this true for the fundamental types like
strings, numbers, tuples, and lists:

max at charmaine:~% python
Python 2.0 (#5, Nov 10 2000, 21:47:15) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> s = ''
>>> i = 0
>>> s == None
0
>>> i == None
0
>>> t = ()
>>> () == None
0
>>> l = []
>>> l == None
0

Only None compares equal to None, and since any reference to None _is_
the unique object of type None, the idiom `x == None' is just as valid
as `x is None'.

Now as a matter of style I would agree with you; I idiomatically use `x
is None' to test if x is None, but as a matter of the language I don't
think this stylistic issue is mandated.  (In other words, the
_technically_ in your response should have been a _according to the
style which I prefer_, which is quite a difference.)

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ All the people in my neighborhood turn around and get mad and sing
\__/ Public Enemy
    The laws list / http://www.alcyone.com/max/physics/laws/
 Laws, rules, principles, effects, paradoxes, etc. in physics.



More information about the Python-list mailing list