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


Groups > comp.lang.python > #103818

Re: What arguments are passed to the __new__ method ?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: What arguments are passed to the __new__ method ?
Date Tue, 1 Mar 2016 13:54:31 -0600
Lines 61
Message-ID <mailman.79.1456862114.20602.python-list@python.org> (permalink)
References <56d5d043$0$632$426a74cc@news.free.fr>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de xqEKfAQVjSTLON/xf39FtQUy3SWZusZIlLmfw4/1DpeA==
Return-Path <eryksun@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'ignored': 0.05; '-1;': 0.09; '__init__': 0.09; 'ast': 0.09; 'fails.': 0.09; 'subject:method': 0.09; 'exception': 0.13; 'def': 0.13; '2016': 0.16; '__new__': 0.16; 'arguments;': 0.16; 'consume': 0.16; 'err': 0.16; 'example).': 0.16; 'overriding': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'case.': 0.18; 'passes': 0.18; 'cc:2**0': 0.20; 'arguments': 0.22; 'null;': 0.22; 'pass': 0.22; 'am,': 0.23; 'second': 0.24; 'implemented': 0.24; 'header :In-Reply-To:1': 0.24; 'equivalent': 0.27; 'subject: ?': 0.27; 'message-id:@mail.gmail.com': 0.27; 'methods.': 0.29; 'skip:_ 10': 0.32; 'statement': 0.32; 'class': 0.33; 'raised': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'expected': 0.35; 'skip:p 30': 0.35; 'according': 0.36; 'but': 0.36; 'received:209.85': 0.36; '(i.e.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'skip:o 20': 0.38; 'subject:the': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'behavior': 0.61; 'strange': 0.63; 'mar': 0.65
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=7de3/Vt8Z6nSrFvgKjJXUzcAruKOurmGCXz3x+wyDfg=; b=i0azmpE44IdUiNRzvuGmaNVk3278J97JmV5QEl/4FhXZwpBaLliVTLMMN3pj+RDzKq sQzFY1mJKuTSoTlezH9Ohn3mlDdtEubj5NwJdSpXMzFUhVaipsO6NWxz99Eua0iXvIDo V18XxM8CIQL7JY7WJeOXlIJnN9FOvXaX6LqvvOy6Uy8opHgMzXd7iGmyztYRtWCbrAyW 1+G28ht/mQUsGGcZR0rAlShyGtHBJPelHBAc/WEb6hXIsVXMyrIUGkTJ+nGZPQq1WhJR OMHKC9GvVt0wgNt5NyI5PraXxuU26u8fjhxUR+aifQWvmHKIqWs7FuFM6zf0oWhcq5NC Vpdg==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=7de3/Vt8Z6nSrFvgKjJXUzcAruKOurmGCXz3x+wyDfg=; b=OIvCTr73M5rV8OBl3ayXKUJArnjVkFbNHTkl/d1ORi7SPtOSg/0nBl86RFz5A1b7JB AlX5Me5ZzBjJF0BcNlzuVulUED4AW/riwcSCMdx/+VjuSLTGrP1TUJ5P3w2E6UWuJiqn ZY5696OHXxhYStBsWr/u7XfMl0OCHwelj60wKfcShOjsDapRYCiBtf4pB7/Cwa4UHRCu Z4ysW/cRT2M32oFhprkYtcjq69INHqp74VaTtP8thTizDkf54XOD5V3n2uEqo7ljrqJz Qx8cJIvfDN0LhAjeTRpc52OtbknMYf+njv4jeg3fehcmyFBuI4JAVgm3jjSodLV9P7aA pQrg==
X-Gm-Message-State AD7BkJKNABz9KWeqaJuccaDNvM+2MonkAJgnpEkBxabGCqRLOIWq1cd3qvdyC027urBNEWhEFsyVAQ7dKgC8BA==
X-Received by 10.50.73.228 with SMTP id o4mr902387igv.32.1456862111139; Tue, 01 Mar 2016 11:55:11 -0800 (PST)
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 <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>
Xref csiph.com comp.lang.python:103818

Show key headers only | View raw


On Tue, Mar 1, 2016 at 11:24 AM, ast <nomail@invalid.com> wrote:
>
> class Premiere:
>
>    def __new__(cls, price):
>        return object.__new__(cls, price)
>
>    def __init__(self, price):
>        pass
>
> p = Premiere(1000)
>
> it fails. It is strange because according to me it is equivalent to:
>
> class Premiere:
>
>    def __init__(self, price):
>         pass
>
> p = Premiere(1000)

The implementation knowns whether a type overrides the __new__ or
__init__ methods. You're expected to consume additional arguments in
this case. However, excess arguments are ignored in object.__new__ if
a type overrides __init__ without overriding __new__ (i.e. your second
example). Excess arguments are also ignored in object.__init__ if a
type overrides __new__ without overriding __init__.

In CPython, this behavior is implemented for object.__new__ by the
following statement in Objects/typeobject.c, object_new:

    if (excess_args(args, kwds) &&
        (type->tp_init == object_init ||
         type->tp_new != object_new)) {
        PyErr_SetString(PyExc_TypeError,
                        "object() takes no parameters");
        return NULL;
    }

An exception is always raised if a type overrides __new__ and passes
extra arguments to object.__new__. No exception is raised for excess
arguments in object.__new__ if a type overrides __init__ but not
__new__. The __init__ method must consume the extra arguments; it must
not pass them to object.__init__.

The behavior for object.__init__ is implemented by the following
statement in Objects/typeobject.c, object_init:

    if (excess_args(args, kwds) &&
        (type->tp_new == object_new ||
         type->tp_init != object_init)) {
        PyErr_SetString(PyExc_TypeError,
                        "object.__init__() takes no parameters");
        err = -1;
    }

An exception is always raised if a type overrides __init__ and passes
extra arguments to object.__init__. No exception is raised for excess
arguments in object.__init__ if a type overrides __new__ but not
__init__. The __new__ method must consume the extra arguments; it must
not pass them to object.__new__.

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


Thread

What arguments are passed to the __new__ method ? "ast" <nomail@invalid.com> - 2016-03-01 18:24 +0100
  Re: What arguments are passed to the __new__ method ? eryk sun <eryksun@gmail.com> - 2016-03-01 13:54 -0600
  Re: What arguments are passed to the __new__ method ? Terry Reedy <tjreedy@udel.edu> - 2016-03-01 15:27 -0500
  Re: What arguments are passed to the __new__ method ? eryk sun <eryksun@gmail.com> - 2016-03-01 15:09 -0600
  Re: What arguments are passed to the __new__ method ? "ast" <nomail@invalid.com> - 2016-03-02 10:23 +0100
    Re: What arguments are passed to the __new__ method ? Steven D'Aprano <steve@pearwood.info> - 2016-03-02 22:02 +1100
  Re: What arguments are passed to the __new__ method ? Peter Pearson <pkpearson@nowhere.invalid> - 2016-03-02 17:40 +0000
    Re: What arguments are passed to the __new__ method ? Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-03-02 17:57 +0000
      Re: What arguments are passed to the __new__ method ? Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-02 11:26 -0700

csiph-web