[CentralOH] Indicating Meaning of Items of Return Values

Brandon Craig Rhodes brandon at rhodesmill.org
Sun Mar 11 23:55:17 CET 2012


jep200404 at columbus.rr.com writes:

> How do you indicate _meaning_?  When returning multiple things such as
> in a tuple or list from a function or method, what are good ways of
> referring to the individual items that convey _meaning?

The usual way would be to return an object.

    class GrailSearchResult(object):
        """..."""

    def foo():
        r = GrailSearchResult()
        r.search = ...
        r.holy = ...
        r.grail = ...
        return r

But if the "usual Python way" of representing a compound value seems too
heavyweight, the "collections.namedtuple" type supports objects that all
have the exact same set of attributes.  It is implemented as a tuple
whose members can also be accessed by attribute names like ".holy" and
".grail" but which is stored as a compact read-only tuple.

-- 
Brandon Craig Rhodes   brandon at rhodesmill.org   http://rhodesmill.org/brandon


More information about the CentralOH mailing list