confused about __new__

K Richard Pixley rich at noir.com
Tue Dec 27 00:27:23 EST 2011


On 12/26/11 20:53 , Steven D'Aprano wrote:
> On Mon, 26 Dec 2011 20:28:26 -0800, K. Richard Pixley wrote:
>
>> 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.
>
> I'm surprised it works in Python3. Try this in Python 2 instead:
>
> Foo.__new__ = staticmethod(__Foo__new__)

Excellent.  Thank you.

--rich




More information about the Python-list mailing list