len() should always return something

Andre Engels andreengels at gmail.com
Sat Jul 25 02:55:40 EDT 2009


On Sat, Jul 25, 2009 at 4:50 AM, 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.

I don't have this problem - when I have a list of one element, that's
what I use - a list with one element. I think things would get much
simpler if you do such a conversion right at the Matlab-Python
interface, rather than changing all kind of things in Python so that
its types are more matlab-like. Just like you shouldn't program Python
by simply translating Java or C into Python idiom, you also shouldn't
program Python by trying to mimic Matlab. Each language has its own
strengths and weaknesses, and moving things over too much in a
simplistic fashion will usually mean that you get the weaknesses of
both and the strengths of neither.

-- 
André Engels, andreengels at gmail.com



More information about the Python-list mailing list