confused about __new__

K. Richard Pixley rich at noir.com
Mon Dec 26 23:28:26 EST 2011


I'm confused about the following.  The idea here is that the set of 
instances of some class are small and finite, so I'd like to create them 
at class creation time, then hijack __new__ to simply return one of the 
preexisting classes instead of creating a new one each call.

This seems to work in python3, but fails in python2 with:

     Foo(23)
TypeError: unbound method __Foo__new__() must be called with Foo 
instance as first argument (got type instance instead)

I don't understand.  Can anyone explain?

--rich
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tst.py
Type: text/x-python-script
Size: 236 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20111226/1322af7d/attachment.bin>


More information about the Python-list mailing list