Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: What arguments are passed to the __new__ method ? Date: Tue, 1 Mar 2016 15:27:48 -0500 Lines: 95 Message-ID: References: <56d5d043$0$632$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de evMxVbh1xhZNwAuPuTovfQ/INN3roPmIq3JG/8uSUyfQ== Return-Path: 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; 'args': 0.04; 'ignored': 0.05; 'method.': 0.05; 'constructor': 0.07; 'defines': 0.07; 'matches': 0.07; 'modifying': 0.07; 'remaining': 0.07; '"if': 0.09; '1000.': 0.09; '__init__': 0.09; 'arg': 0.09; 'ast': 0.09; 'expected.': 0.09; 'fails.': 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:method': 0.09; 'python': 0.10; 'jan': 0.11; 'def': 0.13; 'appropriate': 0.14; '__init__,': 0.16; '__new__': 0.16; 'cls': 0.16; 'invoking': 0.16; 'passed.': 0.16; 'price,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'issue.': 0.20; '(the': 0.22; 'arguments': 0.22; 'doc': 0.22; 'function,': 0.22; 'latter': 0.22; 'ok.': 0.22; 'pass': 0.22; 'seems': 0.23; 'replacing': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'equivalent': 0.27; 'subject: ?': 0.27; 'function': 0.28; 'callable': 0.29; 'it."': 0.29; 'objects': 0.29; 'code': 0.30; 'call.': 0.30; 'entry': 0.31; '"the': 0.32; 'skip:_ 10': 0.32; 'says': 0.32; 'class': 0.33; 'url:python': 0.33; 'errors,': 0.33; 'true.': 0.33; 'open': 0.33; 'case,': 0.34; 'covered': 0.34; 'clear': 0.35; 'instance': 0.35; 'returning': 0.35; 'expected': 0.35; 'according': 0.36; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'depends': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'signature': 0.37; 'self': 0.38; 'wrong': 0.38; 'subject:the': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'url:3': 0.60; 'claim': 0.61; 'skip:n 10': 0.62; 'received:96': 0.63; 'strange': 0.63; 'different': 0.63; 'results': 0.66; 'price': 0.69; '8bit%:21': 0.70; 'receive': 0.71; 'premiere': 0.84; 'signature.': 0.84; 'url:datamodel': 0.84; 'do:': 0.91; 'received:fios.verizon.net': 0.91; 'url:reference': 0.91; 'inheritance,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: <56d5d043$0$632$426a74cc@news.free.fr> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103820 On 3/1/2016 12:24 PM, ast wrote: > Hello > > It's not clear to me what arguments are passed to the > __new__ method. The objects passed to any function are the objects that are passed. The = type and number of objects that *should be* passed depends on the=20 signature of the function. If class C defines __new__, then C.__new__ will receive as args C and=20 all args passed in a C(...) call. So the C(...) call should have args=20 that match those expected by __new__. If C also defines __init__, it=20 will get the same args other than self replacing cls and thus it should=20 have the same signature. The case is covered by in the entry for __new__ in https://docs.python.org/3/reference/datamodel.html#basic-customization "If __new__() returns an instance of cls, then the new instance=E2=80=99s= =20 __init__() method will be invoked like __init__(self[, ...]), where self = is the new instance and the remaining arguments are the same as were=20 passed to __new__()." > class Premiere: > def __new__(cls, price): > return object.__new__(cls) This matches "Typical implementations create a new instance of the class = by invoking the superclass=E2=80=99s __new__() method using super(current= class,=20 cls).__new__(cls[, ...]) with appropriate arguments and then modifying=20 the newly-created instance as necessary before returning it."=20 object.__new__ only takes the cls parameter. > def __init__(self, price): > pass > p =3D Premiere(1000) > > No errors, so it seems that 2 arguments are passed > to __new__, cls and price, and 2 arguments are passed to __init__, self and price -- as documented. > But if i do: > class Premiere: > def __new__(cls, price): > return object.__new__(cls, price) You get an error in current python because you sent an extra arg to=20 object.__new__. If __new__ calls a superclass __new__, then it should=20 only pass the args expected. At one time, object.__new__ would have=20 accepted the price arg and ignored it. This is no longer true > def __init__(self, price): > pass > p =3D Premiere(1000) > > it fails. It is strange because according to me it is equivalent to: Well, 'you' is wrong ;-), because in the following case,=20 Premiere.__new__ is object.__new__, which has a different signature than = __new__ above. > class Premiere: > def __init__(self, price): > pass > p =3D Premiere(1000) > > which is OK. Premiere is callable because it inherits object.__call__. That=20 function, or the implementation of the CALL FUNCTION bytecode, must=20 notice that Premiere.__new__ is object.__new__, by inheritance, and only = pass Premiere and not 1000. The doc entry for __init__ says "The arguments are those passed to the=20 class constructor expression." (The latter is the expression in the=20 code that results in the class call.) This is always true. But since=20 the signature of object.__new__ was restricted, the claim that the same=20 args are sent to __new__ and __init__ seems not to be true. I may open a new doc issue. --=20 Terry Jan Reedy