How to automate accessor definition?
Diez B. Roggisch
deets at nospam.web.de
Sun Mar 21 17:08:58 CET 2010
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. At 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.)
> What is "best practice" for implementing this sort of class
> *succinctly* (i.e. without a lot of repetitive accessor code)?
> Also, one more question concerning syntax. Suppose that i represents
> an instance of this class. Is it possible to define the class to
> support this syntax
> val = i.field
> i.field += 6
> ...rather than this one
> val = i.get_field()
> i.set_field(i.get_field() + 6)
You don't. Python is not Java. So just use instance attributes, and if
you need bhavior when accessing an attribute, introduce a property.
More information about the Python-list