How to automate accessor definition?

Steve Holden steve at holdenweb.com
Sun Mar 21 18:12:23 CET 2010


kj wrote:
> In <mailman.1018.1269125666.23598.python-list at python.org> Chris Rebert <clp2 at rebertia.com> writes:
> 
>> On Sat, Mar 20, 2010 at 3:15 PM, kj <no.email at please.post> wrote:
>>> I need to create a class solely for the purpose of encapsulating
>>> a large number of disparate data items. =C2=A0At the moment I have no
>>> plans for any methods for this class other than the bazillion
>>> accessors required to access these various instance variables.
>>> (In case it matters, this class is meant to be a private helper
>>> class internal to a module, and it won't be subclassed.)
> 
>> If it's just a completely dumb struct-like class, you might consider
>> something like:
>> http://docs.python.org/library/collections.html#collections.namedtuple
> 
> Very cool.  Thanks!  The class I have in mind is *almost* that
> dumb, but performance is a consideration in this case, which may
> rule out namedtuple.  But I'm glad to learn about it; there are
> many places where I can put them to good use.
> 
You may well find that namedtuple is faster than what you put together
yourself, as the collections module is implemented in C.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list