Why would I get a TypeEror?
Steve Holden
steve at holdenweb.com
Wed Jan 12 18:46:32 EST 2005
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
More information about the Python-list
mailing list