473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

__init__ vs __new__

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
Jan 11 '07 #1
1 2953
On 2007-01-11, Daniel Klein <da***********@gmail.comwrote:
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
Jan 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Felix Wiemann | last post by:
Sometimes (but not always) the __new__ method of one of my classes returns an *existing* instance of the class. However, when it does that, the __init__ method of the existing instance is called...
2
by: Steven Bethard | last post by:
Felix Wiemann wrote: > Steven Bethard wrote: >> http://www.python.org/2.2.3/descrintro.html#__new__ > > > I'm just seeing that the web page says: > > | If you return an existing object,...
11
by: Rob Conner | last post by:
I'm still working on my DateTime class from last week... Why does __init__ not get called? The docs at http://www.python.org/dev/doc/devel/ref/customization.html read "If __new__() returns an...
8
by: kelin,zzf818 | last post by:
Hi, Today I read the following sentences, but I can not understand what does the __init__ method of a class do? __init__ is called immediately after an instance of the class is created. It...
11
by: Paulo da Silva | last post by:
I would like to implement something like this: class C1: def __init__(self,xxx): if ... : self.foo = foo self.bar = bar else: self=C1.load(xxx)
4
by: Steven D'Aprano | last post by:
When you call a new-style class, the __new__ method is called with the user-supplied arguments, followed by the __init__ method with the same arguments. I would like to modify the arguments...
25
by: Erik Lind | last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more online and can write simple modules, but I still don't get when __init__ needs to be used as opposed to creating a class...
1
by: Matthew Wilson | last post by:
I have been experimenting with metaclasses lately. It seems possible to define a metaclass by either subclassing type and then either redefining __init__ or __new__. Here's the signature for...
3
by: Torsten Mohr | last post by:
Hi, i have some questions related to new style classes, they look quite useful but i wonder if somebody can give me an example on constructors using __new__ and on using __init__ ? I just see...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.