"newseater" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >> > class Creator >> > def createInstance(cls, *args, **kwargs): >> > anewinstance = cls.__new__(cls, *args, **kwargs) >> > anewinstance.__init__(*args, **kwargs) >> > >> > return anewinstance >> > createInstance = staticmethod(createInstance) >> > >> > >> > can anyone help?? >> >> __new__ is the proper way to do this, but making it work >> is a bit tricky. If you could post the code you tried to >> get to work for __new__(), we could critique it. >> >> The current documentation is in 3.3.1 of the Python >> Reference Manual (Python 2.4 version). In earlier >> versions, there were a couple of other documents >> that did a better (IMO) job of explaining things. >> >> The trick is that __new__ must return an instance. >> It can be a newly created instance (and the doc shows >> how to do this) or an existing instance of any new style >> class. > > from the documentation it was very hard to guess what the "..." was > supposed to be. also i wasn't able to find out how to control if > __init__() would be called on an instance (if i reused that instance.. > and that instance could be of a completely different type).
I believe that __init__() is not called if the instance is not of the same type as the class with the __new__() method. There was a thread on that not too long ago, and I believe that was the resolution: the docs were wrong, __init__ is only called if the returned instance is for the class with the __new__ method. The 2.4 docs seem to say it properly. > eg. i didn't get much out of > http://docs.python.org/ref/customization.html > > from http://gnosis.cx/publish/programming/metaclass_1.html This has nothing to do with metaclasses. Wandering into that will fry your brain. ... > > but that way of creating objects is just plain silly! Yep. If you don't need a metaclass, don't use one. Metaclasses are a special purpose construct. If you need it, you'll know that you need it. Othewise you don't, and you certainly don't here. > I tried looking at the __new__ buildin but this seems to be addressing > old-style objects (which i'm not interested in using). __new__ is only invoked for new style classes; it is not invoked for old style classes.f The following may be helpful: it's a relatively extended article on new style classes that was left out of the 2.4 references because the content had supposedly been absorbed into the mainline documentation. http://www.python.org/2.2.3/descrintro.html >> By the way - when you post code, please use spaces >> for indentation. There are a number of popular mail >> clients that don't play fair with tabs, and people using >> these clients will frequently ignore code that isn't >> properly indented. > > sorry. maybe people should complain to those lousy newsreader authors > instead of everyone having to comply to the lowest standards? I guess > the readers are actually un*x readers although your comment typically > is found in the w*ndows world where everyone is forced into using word > :-) This dead horse has been beaten into a pulp many times. People with newsreaders and mail clients that don't play fair with tabs are not going to change; snide remarks will only reflect on the person making the remark. The major problem is IE, although I've seen other newsreaders and mail clients do the same thing. Also, I don't even have Word on my Windows system; I use Open Office. Being "forced" to use Word is a choice. John Roth > > >> John Roth > -- http://mail.python.org/mailman/listinfo/python-list