Why would I get a TypeEror?

It's me itsme at yahoo.com
Wed Jan 12 13:25:49 EST 2005


Sorry if my question was a little "lazy" and yes, I was asking about the
"lazy evaluation".  :=)

I am surprised about this (and this can be dangerous, I guess).

If this is true, I would run into trouble real quick if I do a:

(1/x,1.0e99)[x==0]

and that's not good.

Something to keep in mind.  :-(


"harold fellermann" <harold.fellermann at upf.edu> wrote in message
news:mailman.578.1105553620.22381.python-list at python.org...
>
> On 12.01.2005, at 18:35, It's me wrote:
>
> > For this code snip:
> >
> > a=3
> > ....
> > b=(1,len(a))[isinstance(a,(list,tuple,dict))]
> >
> > Why would I get a TypeError from the len function?
>
> the problem is, that (1,len(a)) is evaluated, neither what type a
> actually has
> (python has no builtin lazy evaluation like ML). You have to do it this
> way
> instead:
>
> a=3
> ...
> b = isinstance(a,(list,tuple,dict)) and len(a) or 1
>
> - harold -
>
> --
> The opposite of a correct statement is a false statement.
> But the opposite of a profound truth may be another profound truth.
> -- Niels Bohr
>





More information about the Python-list mailing list