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


Groups > comp.lang.python > #70891

Why has __new__ been implemented as a static method?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'causing': 0.04; 'static': 0.04; 'explicitly': 0.05; 'guido': 0.05; 'method.': 0.07; 'implements': 0.09; 'instance.': 0.09; 'logic': 0.09; 'method,': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'subject:method': 0.09; 'python': 0.11; 'def': 0.12; '__new__': 0.16; 'accepts': 0.16; 'cls': 0.16; 'factory': 0.16; 'normally,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skipping': 0.16; ':-)': 0.16; 'all.': 0.16; 'advance.': 0.19; 'implementing': 0.19; 'seems': 0.21; 'header:User-Agent:1': 0.23; 'sort': 0.25; 'class.': 0.26; 'this:': 0.26; 'header:X-Complaints- To:1': 0.27; 'generally': 0.29; 'specified': 0.30; "i'm": 0.30; 'asked': 0.31; "skip:' 10": 0.31; 'allows': 0.31; 'class': 0.32; 'this.': 0.32; 'cases': 0.33; 'guess': 0.33; 'implemented': 0.33; 'actual': 0.34; "i'd": 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'curious': 0.36; 'method': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'two': 0.37; 'easily': 0.37; 'implement': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'skip:u 10': 0.60; 'course.': 0.60; 'most': 0.60; 'new': 0.61; 'teaching': 0.64; 'more': 0.64; 'p.s.': 0.66; 'received:46': 0.66; 'direct': 0.67; 'between': 0.67; 'believe': 0.68; 'natural': 0.68; 'now:': 0.74; 'surprise': 0.74; 'x):': 0.84; 'charset:windows-1250': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Jurko Gospodnetić <jurko.gospodnetic@pke.hr>
Subject Why has __new__ been implemented as a static method?
Date Sat, 03 May 2014 12:37:24 +0200
Organization PKE sistemi d.o.o.
Mime-Version 1.0
Content-Type text/plain; charset=windows-1250; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 46.188.193.20
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9665.1399113463.18130.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1399113463 news.xs4all.nl 2874 [2001:888:2000:d::a6]:50306
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70891

Show key headers only | View raw


   Hi all.

   I was wandering why Python implements its __new__ method as a static 
and not a class method?

   __new__ always accepts a cls parameter, which lead me to believe it 
was a class method. Also, implementing __new__ as a class method seems 
natural when thinking about __new__ as 'a method you call on a class to 
create a new instance of that class'.

   The difference between the two implementations is admittedly not that 
significant. In most cases __new__ would still need to have its cls 
parameter explicitly specified since it is most often called on a class 
and not on an instance.

   Having it implemented as a class method would allow this:

     class X:
       def __new__(cls, x):
         return super().__new__(x)

   instead of having to do this, as we do now:

     class X:
       def __new__(cls, x):
         return super().__new__(cls, x)


   Thinking about this some more - I guess having it as a static method 
allows you to use super() and still create a new 'subclass' when asked 
to instantiate a base class. Perhaps some sort of a factory pattern 
implementation like so:

     class Base:
       def __new__(cls, x):
         actual_class = get_class_for(x)
         return super().__new__(actual_class, x)

   If __new__ were a class method, the same logic could not be so easily 
implemented as you would need to call __new__ on actual_class, and so 
each actual_class would need to implement its own __new__ skipping its 
direct parent's __new__ - yuck... what a mess...

   Could that be the actual use case causing Guido to model __new__ as a 
static method? Or was it something else?


   I'm not suggesting this be changed, of course. I'm just curious as to 
whether I'm not fully understanding something in this part of Python. I 
generally find Python highly intuitive and it's not often something 
about it takes me by surprise like this. :-)

   Thanks in advance.

   Best regards,
     Jurko Gospodnetić

P.S.
   Normally, I'd chalk this issue up under 'bike-shedding', but it came 
up while teaching others about Python and so does not feel right leaving 
it as 'because that's the way it is'. :-)

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


Thread

Why has __new__ been implemented as a static method? Jurko Gospodnetić <jurko.gospodnetic@pke.hr> - 2014-05-03 12:37 +0200
  Re: Why has __new__ been implemented as a static method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-05-03 15:12 +0000
    Re: Why has __new__ been implemented as a static method? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-05-04 11:21 +1200
      Re: Why has __new__ been implemented as a static method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-05-04 03:37 +0000
        Re: Why has __new__ been implemented as a static method? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-05-04 20:03 +1200
          Re: Why has __new__ been implemented as a static method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-05-04 14:16 +0000
            Re: Why has __new__ been implemented as a static method? Rotwang <sg552@hotmail.co.uk> - 2014-05-04 17:24 +0100
            Re: Why has __new__ been implemented as a static method? Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-04 11:05 -0600

csiph-web