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 ?

From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: What arguments are passed to the __new__ method ?
Date 2016-03-01 13:54 -0600
Message-ID <mailman.79.1456862114.20602.python-list@python.org> (permalink)
References <56d5d043$0$632$426a74cc@news.free.fr>

Show all headers | 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