confused about __new__
K Richard Pixley
rich at noir.com
Tue Dec 27 17:19:44 EST 2011
On 12/27/11 12:34 , Ian Kelly wrote:
> On Tue, Dec 27, 2011 at 1:31 PM, K Richard Pixley<rich at noir.com> wrote:
>> On 12/27/11 10:28 , Ian Kelly wrote:
>>>
>>> On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley<rich at noir.com> wrote:
>>>>
>>>> The conceptual leap for me was in recognizing that a class is just an
>>>> object. The best way, (imo, so far), to create a singleton in python is
>>>> to
>>>> use the class itself as the singleton rather than ever instantiating it.
>>>> With a little work, you can even prevent it from ever being
>>>> instantiated.
>>>
>>> I don't think that's actually possible:
>>>
>>> Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
>>> (Intel)] on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>>
>>>>>> class Foo:
>>>
>>> ... def __new__(cls):
>>> ... raise TypeError
>>> ... def __init__(self):
>>> ... raise TypeError
>>> ...
>>>>>>
>>>>>> type(object.__new__(Foo))
>>>
>>> <class '__main__.Foo'>
>>
>>
>> Try:
>>
>> class Foo(object):
>> def __new__(cls):
>> return cls
>
>
> Okay:
>
> Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> class Foo:
> ... def __new__(cls):
> ... return cls
> ...
>>>> f1 = object.__new__(Foo)
>>>> f2 = object.__new__(Foo)
>>>> type(f1), type(f2)
> (<class '__main__.Foo'>,<class '__main__.Foo'>)
>>>> f1 is f2
> False
I'm not seeing why you're using "object.__new__". With Foo(), it seems
fine.
rich at fuji-land.noir.com> python
Python 2.7.2 (default, Dec 12 2011, 13:05:49)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on
darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
... def __new__(cls):
... return cls
...
>>> f1 = Foo()
>>> f2 = Foo()
>>> id(Foo)
4298515984
>>> id(f1)
4298515984
>>> id(f2)
4298515984
rich at fuji-land.noir.com> python3
Python 3.2.2 (default, Dec 26 2011, 15:03:08)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on
darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
... def __new__(cls):
... return cls
...
>>> f1 = Foo()
>>> f2 = Foo()
>>> id(f1)
4298841008
>>> id(f2)
4298841008
>>> f1.stuff = True
>>> f2.stuff
True
>>> id(Foo)
4298841008
Are you trying to demonstrate that I haven't prevented you from
instantiating Foo? If so, then I will cede that point. I certainly
don't know enough about python internals just now to even claim to be
capable of protecting a class from a hostile user. My guess is that
short of a privileged/unprivileged split, or castrating the interpreter
and locking you into it, such a protection would not be possible.
My point was that I can indeed intercept common and convenient usage to
create a lovely singleton semantic. I can't force you to use it. (Nor
do I have any motivation to so do.)
--rich
More information about the Python-list
mailing list