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?

Thanks,


because the interpreter evaluates the tuple (1, len(a)) before applying the indexing to it.

You are trying to be far too clever. The standard way to write this would be:

a = 3
....
b = 1
if isinstance(a,(list,tuple,dict)):
    b = len(a)

Is code length *really* so important? Think carefully ...

regards
 Steve
--
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to