Problem using copy.copy with my own class

Jeffrey Barish jeff_barish at earthlink.net
Tue Apr 22 13:13:43 EDT 2008


(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




More information about the Python-list mailing list