[Tutor] how-to do black magic

Magnus Lyckå magnus@thinkware.se
Fri May 16 09:04:01 2003


At 10:13 2003-05-16 +0200, Adam Groszer wrote:
> From this list I'd like to create a class's properties and getter/setter
>methods with a metaclass.

 From an object oriented design point of view, using pure
data classes, i.e. classes lacking more substantial
methods than getters and setters, is a bad idea.

It seems to me that you only need one class if the
only difference is what attributes you want to store,
but you have shared logic.

If you settle for a syntax of x.set('name', 'Brian')
and x.get('weight') etc, you have a very simple
implementation of a single class. Just feed the
constructor with a list of allowed attributes for
that instance.

You can also use __getattr__ to do special things
when the attr starts with 'get' or 'set'. You don't
*need* a meta class for that.


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program