stylistic question -- optional return value

Erik Max Francis max at
Fri Aug 30 03:30:06 CEST 2002

Stefan Schwarzer wrote:

> Surely it's more direct, but I thought the original poster would like
> another
> solution for its readablity. (I find the object.attr notation a bit
> more
> readable than the dict[key] notation.) Probably it's a matter of
> taste.

In my opinion, returning a custom instance of some dummy class instead
of a dictionary seems _less_ readable.  If I call a function and get
back a dictionary, I immediately know what's going on.  If I call it and
get back an instance of DummyClass, it's much less clear what I'm
supposed to do with the result.

I'm a strong advocate of writing self-documenting code whenever
possible.  Returning an instance with custom attributes involves more
overhead, is slower (albeit by a very small amount), and seems less
clear than just returning a dictionary -- because, after all, if you're
returning an instance, that instance's __dict__ is really what holds the
information anyway.

 Erik Max Francis / max at /
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church /
 A lambda calculus explorer in Python.

More information about the Python-list mailing list