Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18050

Re: confused about __new__

References <MPbKq.47607$cN1.25052@newsfe12.iad> <mailman.4125.1324964928.27778.python-list@python.org> <ErnKq.40489$2e7.4368@newsfe18.iad> <CALwzidm_jc883ShOSmRe7N+5mx6uyKp_eSGvGBJNQOFy46xozA@mail.gmail.com> <4EFA2B0E.2000506@noir.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-12-27 13:34 -0700
Subject Re: confused about __new__
Newsgroups comp.lang.python
Message-ID <mailman.4149.1325018086.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Dec 27, 2011 at 1:31 PM, K Richard Pixley <rich@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@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

confused about __new__ "K. Richard Pixley" <rich@noir.com> - 2011-12-26 20:28 -0800
  Re: confused about __new__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-27 04:53 +0000
    Re: confused about __new__ K Richard Pixley <rich@noir.com> - 2011-12-26 21:27 -0800
  Re: confused about __new__ Fredrik Tolf <fredrik@dolda2000.com> - 2011-12-27 06:48 +0100
    Re: confused about __new__ K Richard Pixley <rich@noir.com> - 2011-12-27 09:41 -0800
      Re: confused about __new__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 11:28 -0700
      Re: confused about __new__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 13:34 -0700
        Re: confused about __new__ K Richard Pixley <rich@noir.com> - 2011-12-27 14:19 -0800
          Re: confused about __new__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 15:43 -0700
      Re: confused about __new__ K Richard Pixley <rich@noir.com> - 2011-12-27 12:31 -0800
  Re: confused about __new__ Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 00:28 -0700
  Re: confused about __new__ Lie Ryan <lie.1296@gmail.com> - 2011-12-27 18:47 +1100
  Re: confused about __new__ Fredrik Tolf <fredrik@dolda2000.com> - 2011-12-27 19:05 +0100

csiph-web