Complaint Dept: My shoes!

Thomas Wouters thomas at
Thu Apr 13 12:48:54 EDT 2000

On Thu, Apr 13, 2000 at 04:20:47PM +0000, musingattheruins at wrote:
> If shoe is None then len(shoe) raises an exception.  Would be nice if
> len(None) was 0 and NOT raise an exeption.
> Example: (Python 1.5.2)
> def show_shoe_wear_to_complaint_dept(name):
>    if name == "Idle":
>       return None
>    else:
>       return "pick up hammer..."
> def hit_on_head(): return "POW!!!"
> shoe = ""
> shoe = show_shoe_wear_to_complaint_dept("Idle")
> if len(shoe)>0: print shoe, hit_on_head()
> If len(shoe) returned 0 then the example would work without error, the
> example above raises an exception whenever Idle shows his shoe to the
> complaint department.
> The work around for the above is....
> shoe = show_shoe_wear_to_complaint_dept("Idle") or ""
> which gets rid of the problem but feels weird.

Or, of course,

    if name == "Idle":
	return ""

You apparently depend on show_shoe_wear_to_complaint_dept returning a string
(or at least something that len() works on... len() only works on sequences
(as it should))

Or perhaps the better solution would be to change:

if len(shoe)>0:


if shoe:

as both None and the empty string are false. It entirely depends on what
else you wish to do with your shoe. (Dont get me started on how Shoe should
actually be a class with it's 'put_on' and 'throw' methods... ;)

Making errors silently go away is not quite the pythonic way. Nothing stops
you, of course, from using:

oldlen = len
def len(object):
    if not object:
        return 0
    return oldlen(object)

or something like

        return len(object)
    except TypeError:
        return 0

if you insist :P

Thomas Wouters <thomas at>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

More information about the Python-list mailing list