len() should always return something

Diez B. Roggisch deets at nospam.web.de
Fri Jul 24 02:57:44 EDT 2009


Dr. Phillip M. Feldman schrieb:
> Some aspects of the Python design are remarkably clever, while others leave
> me perplexed. Here's an example of the latter: Why does len() give an error
> when applied to an int or float? len() should always return something; in
> particular, when applied to a scalar, it should return a value of 1. Of
> course, I can define my own function like this:
> 
> def mylen(x):
>    if isinstance(x,int) or isinstance(x,float): return 1
>    return len(x)
> 
> But, this shouldn't be necessary.

Can you show some example of where that is actually making a piece of 
code more elegant?

Diez



More information about the Python-list mailing list