stylistic question -- optional return value

Stefan Schwarzer sschwarzer at sschwarzer.net
Thu Aug 29 16:01:31 EDT 2002


Hi Erik Max

Erik Max Francis wrote:
> Stefan Schwarzer wrote:
>>A variant of returning a tuple but with more readability could be:
>>
>>-----
>># you may use another name
>>class ValueWithDebugInfo:
>>     pass
>>
>>class X:
>>     def do_something(self):
>>         ...
>>         result = ValueWithDebugInfo()
>>         result.that_what_i_want = ...
>>         result.debug_info = ...
>>         return result
> 
> Why not just skip the middle man and return a dictionary?  After all,
> you're only really using the class instance for its __dict__ attribute
> dictionary anyway.  Seems more direct to just return a dictionary.

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.

Additionally, I tend to rarely use "low-level" data types because with classes it's
a bit easier to change the implementation later. I also tend to use objects with
rather "lean" interfaces; IMHO, a dictionary does too much here.

Stefan




More information about the Python-list mailing list