[CentralOH] Indicating Meaning of Items of Return Values
Thomas Winningham
winningham at gmail.com
Mon Mar 12 01:18:08 CET 2012
i really like brandon's idea of an empty class with overloaded fields.
there's also of course returning a dict although that'll be more
verbose {"search": "name", "holy": "is", "grail": "tim?"} to access
but would be descriptive perhaps, especially in the repl. mark's the
best way to break it out, these dicts will be unordered unless you use
the new ordered dict.
On Sun, Mar 11, 2012 at 6:55 PM, Brandon Craig Rhodes
<brandon at rhodesmill.org> wrote:
> 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
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> http://mail.python.org/mailman/listinfo/centraloh
More information about the CentralOH
mailing list