Friday, 9 August 2013

Is there a shortcut for creating __init__ functions based on class attributes?

Is there a shortcut for creating __init__ functions based on class
attributes?

I'm using sqlalchemy's ORM and I have a bunch of Declarative classes that
with class attributes. For instance:
class Example(Declarative):
id = Column(Integer, primary_key=True)
datum = Column(String(65))
Naturally, my classes are much longer than this example and I have about
two dozen. I'd like to be able to populate their fields at instantiation
time, so it would be nice to have __init__ functions for each class.
The naive way to do this is as follows:
def __init__(self, id, datum):
self.id = id
self.datum = datumi
This gets very tedious. Is there some sort of shortcut? Perhaps I can
exploit Example.__dict__? These are new-style classes.

No comments:

Post a Comment