473,490 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem using copy.copy with my own class

(Pdb) myclass
MyClass( 0, 0, 'A string', 123.45)
(Pdb) copy.copy(myclass)
*** TypeError: TypeError('__new__() takes at least 4 arguments (2 given)',)

I see 4 arguments (actually, 5 because Python is passing cls invisibly to
__new__). Does anyone have an idea what is going on here?

I have not defined __new__ in MyClass above. I can make the problem go away
on one platform by defining __new__ as

return MyClass(self, self.arg1, self.arg2, self.arg3)

but I understand that return value to be the default, so I don't see why
defining that method makes a difference. On another platform, that
definition causes a different problem (I seem to be getting None as the
return value of the copy in some cases).

By the way, I have simplified somewhat the code in the explanation. In case
it might matter, know that there are actually two classes that exhibit this
problem. In one, there is actually a fourth argument in the __new__ method
that has a default value (the float above), which is why the error message
says that __new__ expects *at least* 4 arguments. In the other, the last
argument is a parg collector.

I have also had trouble pickling these classes. I surmise that pickle uses
copy.
--
Jeffrey Barish

Jun 27 '08 #1
7 1745
On Tue, 22 Apr 2008 11:13:43 -0600, Jeffrey Barish wrote:
By the way, I have simplified somewhat the code in the explanation.
Please simplify the code to a minimal example that still has the problem
and *show it to us*. It's hard to spot errors in code that nobody except
you knows.
Jun 27 '08 #2
Marc 'BlackJack' Rintsch wrote:
Please simplify the code to a minimal example that still has the problem
and *show it to us*. It's hard to spot errors in code that nobody except
you knows.
Here it is:

import copy

class Test(int):
def __new__(cls, arg1, arg2):
return int.__new__(cls, arg1)

def __init__(self, arg1, arg2):
self.arg2 = arg2

if __name__ == '__main__':
t = Test(0, 0)
t_copy = copy.copy(t)

Traceback (most recent call last):
File "copytest.py", line 12, in <module>
t_copy = copy.copy(t)
File "/usr/lib/python2.5/copy.py", line 95, in copy
return _reconstruct(x, rv, 0)
File "/usr/lib/python2.5/copy.py", line 322, in _reconstruct
y = callable(*args)
File "/usr/lib/python2.5/copy_reg.py", line 92, in __newobj__
return cls.__new__(cls, *args)
TypeError: __new__() takes exactly 3 arguments (2 given)
--
Jeffrey Barish

Jun 27 '08 #3
Jeffrey Barish wrote:
Marc 'BlackJack' Rintsch wrote:
>Please simplify the code to a minimal example that still has the problem
and *show it to us*. It's hard to spot errors in code that nobody except
you knows.

Here it is:

import copy

class Test(int):
def __new__(cls, arg1, arg2):
^^^^^^^
The exception is saying that copy.copy is not providing this 3rd
argument. I might be a bit dense, but what would arg2 be for? Isn't
arg1 supposed to be the object instance you are copying from? If so,
arg2 is superfluous.

Jun 27 '08 #4
En Thu, 24 Apr 2008 00:32:37 -0300, Michael Torrie <to*****@gmail.com>
escribió:
Jeffrey Barish wrote:
>Marc 'BlackJack' Rintsch wrote:
>>Please simplify the code to a minimal example that still has the
problem
and *show it to us*. It's hard to spot errors in code that nobody
except
you knows.

Here it is:

import copy

class Test(int):
def __new__(cls, arg1, arg2):
^^^^^^^
The exception is saying that copy.copy is not providing this 3rd
argument. I might be a bit dense, but what would arg2 be for? Isn't
arg1 supposed to be the object instance you are copying from? If so,
arg2 is superfluous.
I agree. In case arg2 were really meaningful, use __getnewargs__ (see )

import copy

class Test(int):
def __new__(cls, arg1, arg2):
return int.__new__(cls, arg1)
def __init__(self, arg1, arg2):
self.arg2 = arg2
def __getnewargs__(self):
return int(self), self.arg2

pyt = Test(3, 4)
pyprint type(t), t, t.arg2
<class '__main__.Test'3 4
pyt_copy = copy.copy(t)
pyprint type(t_copy), t_copy, t_copy.arg2
<class '__main__.Test'3 4

But note that subclassing int (and many other builtin types) doesn't work
as expected unless you redefine most operations:

pyx = t + t_copy
pyprint type(x), x
<type 'int'6
pyt += t_copy
pyprint type(t), t
<type 'int'6

--
Gabriel Genellina

Jun 27 '08 #5
En Thu, 24 Apr 2008 01:21:15 -0300, Gabriel Genellina
<ga*******@yahoo.com.arescribió:
I agree. In case arg2 were really meaningful, use __getnewargs__ (see )
I forget to include the link: <http://docs.python.org/lib/pickle-inst.html>

--
Gabriel Genellina

Jun 27 '08 #6
On Apr 23, 9:48*pm, Jeffrey Barish <jeff_bar...@earthlink.netwrote:
>
Here it is:

import copy

class Test(int):
* * def __new__(cls, arg1, arg2):
* * * * return int.__new__(cls, arg1)

* * def __init__(self, arg1, arg2):
* * * * self.arg2 = arg2

if __name__ == '__main__':
* * t = Test(0, 0)
* * t_copy = copy.copy(t)
First off, inheriting from a basic builtin type such as int and
changing its constructor's signature is not typical; you should
rethink your design unless you know what you're doing.

One way to make this work is to define the special __copy__ method
[1], specifying explicitly how to create a copy of a Test instance:

class Test(int):
...
def __copy__(self):
return Test(int(self), self.arg2)

The copy.copy() function looks for this special method and invokes it
if it's defined. Normally (i.e. for pure Python classes that don't
subclass a builtin other than object) copy.copy() is smart enough to
know how to create a copy without an explicit __copy__ method, so in
general you don't have to define it for every class that has to be
copyable.
Traceback (most recent call last):
* File "copytest.py", line 12, in <module>
* * t_copy = copy.copy(t)
* File "/usr/lib/python2.5/copy.py", line 95, in copy
* * return _reconstruct(x, rv, 0)
* File "/usr/lib/python2.5/copy.py", line 322, in _reconstruct
* * y = callable(*args)
* File "/usr/lib/python2.5/copy_reg.py", line 92, in __newobj__
* * return cls.__new__(cls, *args)
TypeError: __new__() takes exactly 3 arguments (2 given)
The traceback is not obvious indeed. It turns out it involves calling
the arcane __reduce_ex__ special method [2] defined for int, which
returns a tuple of 5 items; the second is the tuple
(<class '__main__.Test'>, 0) and these are the arguments passed to
Test.__new__. So another way of fixing it is keep Test.__new__
compatible with int.__new__ by making optional all arguments after the
first:

class Test(int):
def __new__(cls, arg1, arg2=None):
return int.__new__(cls, arg1)

# don't need to define __copy__ now

from copy import copy
t = Test(0, 0)
assert copy(t) == t

As a sidenote, your class works fine without changing anything when
pickling/unpickling instead of copying, although pickle calls
__reduce_ex__ too:

from pickle import dumps,loads
t = Test(0, 0)
assert loads(dumps(t)) == t

Perhaps someone more knowledgeable can explain the subtle differences
between pickling and copying here.

George

[1] http://docs.python.org/lib/module-copy.html
[2] http://docs.python.org/lib/node320.html
Jun 27 '08 #7
George Sakkis wrote:
First off, inheriting from a basic builtin type such as int and
changing its constructor's signature is not typical; you should
rethink your design unless you know what you're doing.
Nah, I would never claim to know what I'm doing. However, I have to say
that I have been finding this technique very useful. When I started
developing this program, I used an int. Then I discovered that I needed to
have a string associated with the int. By subclassing int to add a string,
I managed to make the change transparent to the code I had already written.
Only the new code that needed the associated string knew that it was
available. In another case, I subclassed str so that I could have a long
form for a string (e.g., a full name attached to the surname). Are these
applications of subclassing bad form? What is the motivation for your
warning?
One way to make this work is to define the special __copy__ method
[1], specifying explicitly how to create a copy of a Test instance:

Normally (i.e. for pure Python classes that don't
subclass a builtin other than object) copy.copy() is smart enough to
know how to create a copy without an explicit __copy__ method, so in
general you don't have to define it for every class that has to be
copyable.
Yes, I noted in my original posting (which seems to have fallen off this
thread) that the __copy__method solved the problem (at least on one
platform). However, I was wondering why it was necessary when what I was
defining was supposedly the default action. Thanks for your explanation.
The traceback is not obvious indeed. It turns out it involves calling
the arcane __reduce_ex__ special method [2] defined for int, which
returns a tuple of 5 items; the second is the tuple
(<class '__main__.Test'>, 0) and these are the arguments passed to
Test.__new__. So another way of fixing it is keep Test.__new__
compatible with int.__new__ by making optional all arguments after the
first:
This suggestion is very interesting. It seems to be an alternative to the
solution suggested by Gabriel. The reference that Gabriel provided
includes the statement:

Instances of a new-style type C are created using

obj = C.__new__(C, *args)

where args is the result of calling __getnewargs__() on the original
object; if there is no __getnewargs__(), an empty tuple is assumed.

Gabriel's solution using __getnewargs__ assures that args receives a
non-empty tuple. Your solution renders the empty tuple impotent by
specifying default values. Correct me if I am wrong.

I would be interested in a translation into English of the following
statement from the same reference that Gabriel provided:

Implementing this method [i.e., __getnewargs__] is needed if the
type establishes some internal invariants when the instance is
created, or if the memory allocation is affected by the values
passed to the __new__() method for the type (as it is for tuples
and strings).

What is an "internal invariant"? How do I know when the memory allocation
is affected? Does my Test class affect the memory allocation?
As a sidenote, your class works fine without changing anything when
pickling/unpickling instead of copying, although pickle calls
__reduce_ex__ too:

from pickle import dumps,loads
t = Test(0, 0)
assert loads(dumps(t)) == t

Perhaps someone more knowledgeable can explain the subtle differences
between pickling and copying here.
I have a situation in the full program where pickling seems to be failing in
the same manner as copy, but I have not been able yet to reproduce the
problem in a simple test program.

Thanks to all for your comments.
--
Jeffrey Barish

Jun 27 '08 #8

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

Similar topics

4
1795
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
3
1759
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown below and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you...
0
3907
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
11
1894
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
7
2688
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
9
3561
by: David | last post by:
Hi all, I posted my question two days ago, and tried to solve this problem. but until now I didn't solve that. and I cut my codes so maybe this time it is more readable. ...
13
2434
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
2
1868
by: saxman | last post by:
Hi everyone, I'm writing a class that inherits from std::vector in order to add some additional functionality. I'm having a compiler problem, however, when I have a function that returns this...
5
2877
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
0
6974
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
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
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...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.