stylistic question -- optional return value

Erik Max Francis max at alcyone.com
Thu Aug 29 21:30:06 EDT 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 alcyone.com / http://www.alcyone.com/max/
 __ 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 / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list