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


Groups > comp.lang.python > #18051

Re: confused about __new__

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rich@noir.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python': 0.08; '>>>>': 0.09; 'am,': 0.12; 'win32': 0.12; 'def': 0.13; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '--rich': 0.16; 'foo(object):': 0.16; 'from:addr:noir.com': 0.16; 'from:addr:rich': 0.16; 'message-id:@noir.com': 0.16; 'received:198.144': 0.16; 'received:198.144.209': 0.16; 'received:198.144.209.5': 0.16; 'received:noir.com': 0.16; 'recognizing': 0.16; 'singleton': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '3.2': 0.18; 'cc:no real name:2**0': 0.20; 'dec': 0.22; 'feb': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'work,': 0.26; 'raise': 0.28; 'bit': 0.28; '27,': 0.29; 'cc:addr:python.org': 0.29; 'class': 0.29; 'kelly': 0.30; 'object.': 0.30; 'received:198': 0.32; 'tue,': 0.32; 'header:User- Agent:1': 0.33; 'actually': 0.33; 'rather': 0.33; 'it.': 0.34; 'richard': 0.34; 'try:': 0.34; '...': 0.36; 'received:192': 0.37; 'think': 0.37; 'skip:_ 10': 0.37; 'being': 0.39; 'received:192.168': 0.40; 'more': 0.61; 'type': 0.61; '2011': 0.61; 'ever': 0.65; 'conceptual': 0.67; '10:28': 0.84; 'received:192.168.1.7': 0.84
Date Tue, 27 Dec 2011 12:31:10 -0800
From K Richard Pixley <rich@noir.com>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0) Gecko/20111105 Thunderbird/8.0
MIME-Version 1.0
To Ian Kelly <ian.g.kelly@gmail.com>
Subject 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>
In-Reply-To <CALwzidm_jc883ShOSmRe7N+5mx6uyKp_eSGvGBJNQOFy46xozA@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4150.1325018213.27778.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1325018213 news.xs4all.nl 6964 [2001:888:2000:d::a6]:52882
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18051

Show key headers only | View raw


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

--rich

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