changing how instances are "created"

newseater kbilsted at hotmail.com
Sun Jun 12 19:36:22 EDT 2005



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...




More information about the Python-list mailing list