preventing creation of instance

Peter Hansen peter at engcorp.com
Wed Jun 18 08:42:36 EDT 2003


Mirko Koenig wrote:
> 
> On Wed, 18 Jun 2003 02:44:32 +0200, Jeff Epler wrote:
> 
> > import weakref
> >
> > class test(object):
> >       names = weakref.WeakValueDictionary() def __init__(self, name):
> >               pass
> >
> >       def __new__(cls, name):
> >               print "new", cls.names.keys(), name
> >               if cls.names.has_key(name): return None ret = super(test,
> >               cls).__new__(cls, name) cls.names[name] = ret return ret
> 
> Is using __new__ a common way in programming python like the contructor
> in c++ or is it seldom used ?

Perhaps the best answer is that it's *extremely* "new", no pun intended.
It was only just added to the language, and relatively few people have
probably tried using it, let alone figured out how to judge the best
time to use it, and when not to.

And many of us are still programming with older versions that didn't 
have that feature.

-Peter




More information about the Python-list mailing list