stylistic question -- optional return value

Stefan Schwarzer sschwarzer at sschwarzer.net
Wed Aug 28 22:12:27 CEST 2002


Hi Andrew

Andrew Koenig wrote:
> Hans> Still, I believe that rewriting the function (or splitting it up)
> Hans> might be a better idea. <0.3 wink>
> 
> I've considered that, and I really don't think it works in this case.

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
-----

> You might think of the optional result as being similar to debugging
> information -- you really don't want to put the code that getnerates
> debugging information into a separate function, because then it will
> surely diverge from the code about which it is providing the information.

If you need it for debugging I rather recommend other approaches. How about

-----
class X:
     def __init__(self):
         self._debug_log = []
         ...

     def do_something(self):
         ...
         self._debug_log.append(debug_info)
         return result

x = X()
y = x.do_something()
print x._debug_log   # what has happened?
-----

Stefan




More information about the Python-list mailing list