A tuple in order to pass returned values ?

alex23 wuwei23 at gmail.com
Mon Oct 10 00:02:40 EDT 2011


Jean-Michel Pichavant <jeanmic... at sequans.com> wrote:
> However, I'm not sure it fixes the main issue: unpacking. Unpacking
> prevents you from adding any additional fields to your 'tuple' without
> breaking any line of code that was unpacking the tuple (to oppose to
> accessing an object attribute).

Generally, if it's a small, known, unlikely-to-change structure, I'd
use a tuple. For anything else I'd use a class, namedtuple or bunch.

However, pre-namedtuples I'd usually abstract away the field
referencing with indices and lambdas:

   name = 0
   role = 1
   name_role = lambda t: (t[name], t[role])

   name, role = name_role(record)

etc



More information about the Python-list mailing list