Why would I get a TypeEror?

harold fellermann harold.fellermann at upf.edu
Wed Jan 12 13:16:35 EST 2005


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