len() should always return something

Rhodri James rhodri at wildebst.demon.co.uk
Sat Jul 25 15:53:51 EDT 2009


On Sat, 25 Jul 2009 03:50:54 +0100, Dr. Phillip M. Feldman  
<pfeldman at verizon.net> wrote:

>
> Here's a simple-minded example:
>
> def dumbfunc(xs):
>    for x in xs:
>       print x
>
> This function works fine if xs is a list of floats, but not if it is  
> single
> float.  It can be made to work as follows:
>
> def dumbfunc(xs):
>    if isinstance(xs,(int,float,complex)): xs= [xs]
>    for x in xs:
>       print x
>
> Having to put such extra logic into practically every function is one of  
> the
> annoying things about Python.

If you persist in treating <language 1> as if it was <language 2>, then
your code will always be ugly, and often buggy.  Unless we're talking
natural languages, in which case Yoda-like you will sound.

Fundamentally, your problem is your assertion that it is reasonable to
allow users to treat a single object as if it were wrapped in a list.
In Python, it is not reasonable, for the reasons that you are spending
so much time complaining about.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list