newseater wrote: > > Robert Kern wrote: > >>newseater wrote: >> >>>Hello. I need to be able to control how objects are created. Sometimes >>>when creating an object, i want to reuse another object instead. I've >>>searched for factory method implementations and singleton >>>implementations. They were too much of a hack! >>> >>>My first attempt of doing this was to play around with the __new__() >>>method. But i didn't quite succed. Then i came up with making a static >>>method which can create my instances or re-use other instances. >>>However, the below code I cannot get to work :( >>> >>>class Creator >>> def createInstance(cls, *args, **kwargs): >>> anewinstance = cls.__new__(cls, *args, **kwargs) >>> anewinstance.__init__(*args, **kwargs) >>> >>> return anewinstance >>> createInstance = staticmethod(createInstance) >> >>You want a classmethod, not a staticmethod. > > why do i want that? how should the code look like? currently the > objects fail to get initialized etc...
A staticmethod does not take a cls argument. It is essentially just a function that is attached to a class. class Something(object): def foo(x, y, z): print x, y, z foo = staticmethod(foo) A classmethod does that a cls argument. class Creator(object): def createInstance(cls, *args, **kwds): pass createInstance = classmethod(createInstance) As for the desired content of the classmethod, I don't care to speculate. I usually just use the Borg pattern or a factory or any one of the Singleton implementations floating around. They are no more hacky than this, but they have the added benefit of working. -- Robert Kern [EMAIL PROTECTED] "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter -- http://mail.python.org/mailman/listinfo/python-list