◐ Shell
clean mode source ↗

Message 201024 - Python tracker

When __getattr__ is implemented without also implementing __copy__ and __deepcopy__ trying to (deep)copy the class fails.

>>> import copy
>>> class foo():
...   def __getattr__(self, attr):
...     return None
...
>>> f = foo()
>>> copy(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

The copy module checks if a class has implemented __copy__ using hasattr:

if hasattr(x, '__copy__'):
  ...

An easy fix would be to use:

if getattr(x, '__copy__', None):
  ...

In Python 3 this change has already been made.