[Tutor] inheritance/classmethods/metaclasses
Kent Johnson
kent37 at tds.net
Thu Jun 19 17:44:52 CEST 2008
On Thu, Jun 19, 2008 at 9:50 AM, Eric Abrahamsen
<eric at ericabrahamsen.net> wrote:
> I'm making a few classes for a very simple ORM, where the classes reflect
> objects that I'm persisting via pickle. I'd like to add and delete instances
> via add() and delete() classmethods, so that the classes can keep track of
> the comings and goings of their instances.
What you have almost works. Try this:
class Model(object):
@classmethod
def add(cls, *args):
inst = cls(*args)
cls.instances.append(inst)
class File(Model):
instances = []
def __init__(self, *args):
self.filename = args[0]
self.keyword = args[1]
File.add('somefilename','keywordforthisfile')
print File.instances
You have to define instances in each class; a metaclass could do that
for you. Anyway I hope this gets you a little farther.
Kent
More information about the Tutor
mailing list