stylistic question -- optional return value

Stefan Schwarzer sschwarzer at sschwarzer.net
Sat Aug 31 00:17:34 CEST 2002


Hi Erik Max

Erik Max Francis wrote:
> Stefan Schwarzer wrote:
> 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.

How do you know that you really got a dictionary and not some object of a class
that looks like a dictionary at first sight? IMHO, you have to define the interface
anyway.

> I'm a strong advocate of writing self-documenting code whenever
> possible.

I think the same of me. :-)

 > 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

I think that depends on the circumstances. As a guideline, I would use a custom
class if the return value has an abstract meaning that should be covered by a class.

 > -- because, after all, if you're
> returning an instance, that instance's __dict__ is really what holds the
> information anyway.

Perhaps that's an implementation-related aspect that shouldn't be taken too
seriously :-)

Stefan




More information about the Python-list mailing list