Dynamic class construction?

Nic Williams nic at csee.uq.edu.au
Sun Jan 30 19:10:17 EST 2000


In article <38948F0E.36AA2548 at math.okstate.edu>,
  "David C. Ullrich" <ullrich at math.okstate.edu> wrote:
>
[cut]
>     I don't know that I want to - I was wondering if I needed to.
> Couldn't hurt.

Class versioning might be one reason: many classes with the same
internal name that represent different versions of a conceptually single
class. Instead of storing them by individual external name, perhaps a
list could be used.

Account=[newClass('Account',...), newClass('Account',...), ...]
# and a version of the Account class is obtained by indexing the
# global Account list
account0=Account[0](<parameters of constructor>)
account1=Account[1](...)
account1.__name__ == account0.__name__
1

Or whatever. If it makes some sense to have the internal and external
names be different, go for it.
>
>     I take it that simply putting a "pass" in the inner
> class definition and then setting attributes _is_ the
> "right" thing? It wasn't the first thing I thought of,
> but it was the first thing that worked.

You could also generate a large string that represents the class
definition and then exec(...) the code, but that is just plain
silliness.

I think using pass, and then setting previously defined functions as
methods to the class is ok. Just use lots of comments :-)

Nic


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list