__init__ vs __new__

Discussion in 'Python' started by Daniel Klein, Jan 11, 2007.

  1. Daniel Klein

    Daniel Klein Guest

    I've have a program that is using both of the methods below (in
    different classes of course) for initializing the class. The example
    below shows a class with the 2 methods with one commented out.

    class JsubroutineParameters(list):
    "Represents a list of arguments for external subroutine calls."
    # We only need to override methods which add objects to the list.

    def __init__(self, alist = []):
    for objekt in alist: _validateParameter(objekt)
    self.extend(alist)

    #def __new__(cls, alist = []):
    # for objekt in alist: _validateParameter(objekt)
    # return list.__new__(cls, alist)

    I don't really notice any behavioral difference. Is there in fact any
    difference in using one over the other? Performance? Side effects? ???

    I am using Python version 2.5.

    Thanks,
    Daniel Klein
     
    Daniel Klein, Jan 11, 2007
    #1
    1. Advertising

  2. Daniel Klein

    Neil Cerutti Guest

    On 2007-01-11, Daniel Klein <> wrote:
    > I've have a program that is using both of the methods below (in
    > different classes of course) for initializing the class. The
    > example below shows a class with the 2 methods with one
    > commented out.
    >
    > class JsubroutineParameters(list):
    > "Represents a list of arguments for external subroutine calls."
    > # We only need to override methods which add objects to the list.
    >
    > def __init__(self, alist = []):
    > for objekt in alist: _validateParameter(objekt)
    > self.extend(alist)
    >
    > #def __new__(cls, alist = []):
    > # for objekt in alist: _validateParameter(objekt)
    > # return list.__new__(cls, alist)
    >
    > I don't really notice any behavioral difference. Is there in
    > fact any difference in using one over the other? Performance?
    > Side effects? ???
    >
    > I am using Python version 2.5.


    The second version doesn't work the way you might be assuming.

    Guido's paper says that mutable builtins like list have a dummy
    __new__ static method. So 'return list.__new__(cls, alist)' does
    nothing. It seems to work because the base class __init__
    performs the initialization (assuming your __init__ above is
    commented out). You can see this by providing a dummy __init__.

    class Example(list):
    def __new__(cls, alist):
    return list.__new__(cls, alist)
    def __init__(self, alist):
    pass

    >>> a = Example(range(5))
    >>> a

    []

    There is no need to use __new__ for mutable builtin types. Since
    all you want from this construction is a side-effect, you can
    nevertheless use it in this case.

    Your __init__ should call the base-class __init__.

    It's usually a bad idea to provide mutable types as default
    arguments. Since you aren't modifying alist in __init__ you can
    get away with it here, but getting in the habit of using the
    below idiom might save you from a "gotcha" someday.

    class JsubroutineParameters(list):
    def __init__(self, alist=None):
    if alist is None:
    alist = []
    for objekt in alist: _validateParameter(objekt)
    list.__init__(self, alist)

    You will no longer need to call append.

    --
    Neil Cerutti
     
    Neil Cerutti, Jan 11, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chenyu
    Replies:
    1
    Views:
    752
    Alex Martelli
    Nov 7, 2003
  2. Felix Wiemann
    Replies:
    9
    Views:
    394
    Martin
    Feb 16, 2005
  3. Steven Bethard
    Replies:
    2
    Views:
    459
    Steven Bethard
    Feb 16, 2005
  4. Edward C. Jones

    __new__, __init__ and pickle

    Edward C. Jones, Jun 30, 2005, in forum: Python
    Replies:
    0
    Views:
    383
    Edward C. Jones
    Jun 30, 2005
  5. Steven D'Aprano

    Modify arguments between __new__ and __init__

    Steven D'Aprano, Dec 23, 2007, in forum: Python
    Replies:
    4
    Views:
    309
    Steven D'Aprano
    Dec 23, 2007
Loading...

Share This Page