bug in copy.deepcopy or in getattr or in my understanding?

Emin emin.shopper at gmail.com
Thu Jan 4 15:26:00 EST 2007


Dear experts,

I got some unexpected behavior in getattr and copy.deepcopy (see
transcript below). I'm not sure if this is actually a bug in
copy.deepcopy or if I'm doing something too magical with getattr.
Comments would be appreciated.

Thanks,
-Emin

#########  Transcript follows ##################


Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> # THE FOLLOWING BREAKS
>>> class a:
... 	def foo(self):
... 		print 'hi'
...
>>> class b(a):
... 	def __init__(self):
... 		self.y = getattr(self,'foo')
...
>>> c = b()
>>> import copy
>>> copy.deepcopy(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python25\lib\copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "c:\Python25\lib\copy.py", line 291, in _deepcopy_inst
    state = deepcopy(state, memo)
  File "c:\Python25\lib\copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "c:\Python25\lib\copy.py", line 254, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "c:\Python25\lib\copy.py", line 189, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "c:\Python25\lib\copy.py", line 322, in _reconstruct
    y = callable(*args)
  File "c:\Python25\lib\copy_reg.py", line 92, in __newobj__
    return cls.__new__(cls, *args)
TypeError: instancemethod expected at least 2 arguments, got 0
>>> # THE FOLLOWING WORKS
>>> class b(a):
... 	def __init__(self):
... 		self.x = self.__class__.__bases__[0].__dict__['foo']
... 
>>> c=b()
>>> copy.deepcopy(c)
<__main__.b instance at 0x00EADE18>




More information about the Python-list mailing list