[Tutor] question concerning deepcopy

Gregor Lingl glingl at aon.at
Fri Apr 28 18:44:01 CEST 2006


Hi all of you,

I've some Vector class, which is a subclass of tuple and which is 
working satisfactorily since long in different contexts. Now I've 
constructed objects with attributes of Vec-type, which I wanted to 
deepcopy. But that doesn't work, because I can't (deep)copy Vec-s:

 >>> from copy import deepcopy
 >>> class Vec(tuple):
	def __new__(cls, x, y):
		return tuple.__new__(cls, (x,y))
	def __abs__(self):
		return (self[0]**2+self[1]**2)**0.5
         ## more methods ...

	
 >>> a=Vec(3,4)
 >>> abs(a)
5.0
 >>> b = deepcopy(a)

Traceback (most recent call last):
   File "<pyshell#13>", line 1, in -toplevel-
     b = deepcopy(a)
   File "C:\Python24\lib\copy.py", line 204, in deepcopy
     y = _reconstruct(x, rv, 1, memo)
   File "C:\Python24\lib\copy.py", line 336, in _reconstruct
     y = callable(*args)
   File "C:\Python24\lib\copy_reg.py", line 92, in __newobj__
     return cls.__new__(cls, *args)
TypeError: __new__() takes exactly 3 arguments (2 given)

Explanation? Remedy? Bug? Workaround?

Regards,
Gregor


-- 
Gregor Lingl
Reisnerstrasse 3/19
A-1030 Wien

Telefon: +43 1 713 33 98
Mobil:   +43 664 140 35 27

Website: python4kids.net


More information about the Tutor mailing list