Time for action creating instances
The next method we look at is the constructor the
__init__() method. It will be used to create individual instances of an entity. The constructor can be called in two ways:
- With a single
idargument, in which case, an existing record will be retrieved from the database and the instance initialized with the column values of this record, or
- With a number of keyword arguments to create a new instance and save this as a new database record
The code to implement this behavior looks like the following:
def __init__(self,id=None,**kw): for k in kw: if not k in self.__class__.columns : raise KeyError("unknown column") cursor=self.threadlocal.connection.cursor() if id: if len(kw): raise KeyError("columns ...