spir wrote:
Le Sat, 16 May 2009 17:53:09 +1000, Nick Coghlan
s'exprima ainsi: Since option 1 will work for any arbitrary object, while option 2 is limited to objects which support copy.copy (or copy.deepcopy, depending on the copying semantics chosen), the "late evaluation of default arguments" (option 1) approach is the more promising angle of attack by far.
Which objects do not support copy.deepcopy? Really surprised. I used and still use it in personal projects for rather complicated custom objects* and it works as expected.
Bound methods for one:
class C(object): ... def m(): pass ... copy.copy(C().m) Traceback (most recent call last): File "<stdin>", line 1, in <module> 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: instancemethod expected at least 2 arguments, got 0 copy.deepcopy(C().m) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.5/copy.py", line 189, in deepcopy y = _reconstruct(x, rv, 1, memo) 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: instancemethod expected at least 2 arguments, got 0
Copyability really can't be assumed for Python objects - the copy module does an excellent job of making it "just work" for most objects, but it really isn't any more fundamental a property than being hashable. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------