473,386 Members | 1,793 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,386 software developers and data experts.

__new__ to create copy

Hi,

I want to create a copy of an object from out of its base class:

class A(object):
def copy(self):
....

class B(A):
....
b = B()
b.copy()


I'm not sure how to do this:

def copy(self):
cpy = self.__new__(self.__class__)
return cpy

seems not to call the constructor __init__().

How is such a thing done correctly? Where is the exact difference between
__new__ and __init__?
Thanks
Uwe
Jul 18 '05 #1
2 1476
I do object copies this way:

import copy

a = A()
aa = copy.copy(a)

or

aa = copy.deepcopy(a)

You can wrap this up into a method of course, which then can decide whether
to do a deep copy or not.

HTH
Franz GEIGER
"Uwe Mayer" <me*****@hadiko.de> schrieb im Newsbeitrag
news:bv**********@news.rz.uni-karlsruhe.de...
Hi,

I want to create a copy of an object from out of its base class:

class A(object):
def copy(self):
...

class B(A):
...
b = B()
b.copy()


I'm not sure how to do this:

def copy(self):
cpy = self.__new__(self.__class__)
return cpy

seems not to call the constructor __init__().

How is such a thing done correctly? Where is the exact difference between
__new__ and __init__?
Thanks
Uwe

Jul 18 '05 #2
> How is such a thing done correctly? Where is the exact difference between
__new__ and __init__?


For some reason, I'm not finding the doc page, but I am pretty sure the
below is the case.
If __new__ exists, it will be called.
If __new__ exists, it must call __init__ for __init__ to be called.
If __new__ doesn't exist, __init__ will be called, if it exists.
How I usually copy my classes:

class blah:
def __init__(self, arg1, arg2, ...):
self.arg1 = arg1
self.arg2 = arg2
...
def copy(self):
return blah(self.arg1, self.arg2, ...)

It may not be pretty, but it works.

- Josiah
Jul 18 '05 #3

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...
5
by: could ildg | last post by:
As there is already __init__, why need a __new__? What can __new__ give us while __init__ can't? In what situations we should use __new__? And in what situations we must use __new__? Can __new__...
3
by: James Stroud | last post by:
Hello All, I'm running 2.3.4 I was reading the documentation for classes & types http://www.python.org/2.2.3/descrintro.html And stumbled on this paragraph: """ __new__ must return an...
1
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class...
5
by: Ken Schutte | last post by:
Hi, I'm been trying to create some custom classes derived from some of python's built-in types, like int and list, etc. I've run into some trouble, which I could explain with a couple simple...
2
by: jeremito | last post by:
I am subclassing the array class and have __new__ to initialize and create my class. In that class I create not only do I create an array object, but I also create some other data in __new__ I...
18
by: Paulo da Silva | last post by:
Sorry to put here too many questions about __init__ __new__ stuff but I always found a new problem when using them. I have searched for simple __new__ docs on how to do the basic things but find...
5
by: Sandra-24 | last post by:
Ok here's the problem, I'm modifying a 3rd party library (boto) to have more specific exceptions. I want to change S3ResponseError into about 30 more specific errors. Preferably I want to do this...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.