stylistic question -- optional return value
ark at research.att.com
Tue Sep 3 09:20:50 EDT 2002
Bengt> What about defining a class to represent the "data structure,"
Bengt> and passing a reference to an instance of that to your various
Bengt> routines, so that they can populate the structure themselves
Bengt> (via methods/attributes/properties as convenient) instead of
Bengt> returning data for a caller to do the populating with?
That's certainly a possibility, but it's a little heavyweight.
Of the various alternatives I've heard so far, the one I like best is simply
using None to represent the optional information. That is, I return either
(x, y) or (x, None) depending on whether y exists.
Bengt> BTW, you mentioned recursion in another part of the thread. Is
Bengt> that as in recursive descent? An exception is an interesting
Bengt> alternative way to return a first match, with no need to return
Bengt> anything from unsuccessful visits.
Yes, it is a species of recursive descent. What I'm doing, though,
is returning generators at each stage. That way I cover all of the
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark
More information about the Python-list