Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python': 0.08; 'am,': 0.12; 'win32': 0.12; 'def': 0.13; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; 'foo(object):': 0.16; 'it..': 0.16; 'recognizing': 0.16; 'singleton': 0.16; '\xa0def': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 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; 'message-id:@mail.gmail.com': 0.28; '27,': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'class': 0.29; 'kelly': 0.30; 'object.': 0.30; 'tue,': 0.32; 'actually': 0.33; 'rather': 0.33; 'richard': 0.34; 'received:209.85.212': 0.34; 'try:': 0.34; '...': 0.36; 'received:google.com': 0.37; 'think': 0.37; 'skip:_ 10': 0.37; 'received:209.85': 0.38; 'being': 0.39; 'received:209': 0.40; 'more': 0.61; 'type': 0.61; '2011': 0.61; 'ever': 0.65; 'conceptual': 0.67; '10:28': 0.84; 'to:addr:rich': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=16L2QbqQSvbjoqmv7h4fubsD90TqYi5AetVcAXTiB84=; b=vNcJmPheeEetiFEk4Bw0YwzZ7dahOfdvkv4Oash8D53oi/Jf5HWZ1OJ0md3uIZx1RU u7gBu/0SYJYeXvJcnwjrXW1lFkj0CVpgZjlW+cZokXfB0//Dpb0byiBGRA3/Ew9NuZMC YFbFM6/kkKJvnWf8pWLESms1Fb2rvAbT71fvQ= MIME-Version: 1.0 In-Reply-To: <4EFA2B0E.2000506@noir.com> References: <4EFA2B0E.2000506@noir.com> From: Ian Kelly Date: Tue, 27 Dec 2011 13:34:08 -0700 Subject: Re: confused about __new__ To: K Richard Pixley Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325018086 news.xs4all.nl 6972 [2001:888:2000:d::a6]:47333 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18050 On Tue, Dec 27, 2011 at 1:31 PM, K Richard Pixley wrote: > On 12/27/11 10:28 , Ian Kelly wrote: >> >> On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley =A0wro= te: >>> >>> The conceptual leap for me was in recognizing that a class is just an >>> object. =A0The 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= . >>> =A0With 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: >> >> ... =A0 =A0 def __new__(cls): >> ... =A0 =A0 =A0 =A0 raise TypeError >> ... =A0 =A0 def __init__(self): >> ... =A0 =A0 =A0 =A0 raise TypeError >> ... >>>>> >>>>> type(object.__new__(Foo)) >> >> > > > Try: > > class Foo(object): > =A0 =A0def __new__(cls): > =A0 =A0 =A0 =A0return 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 =3D object.__new__(Foo) >>> f2 =3D object.__new__(Foo) >>> type(f1), type(f2) (, ) >>> f1 is f2 False