Challenge supporting custom deepcopy with inheritance
Aahz
aahz at pythoncraft.com
Wed Jun 3 19:45:39 EDT 2009
In article <mailman.1077.1244061915.8015.python-list at python.org>,
Michael H. Goldwasser <goldwamh at slu.edu> wrote:
>On June 2, 2009, Aahz wrote:
>>Michael Goldwasser:
>>>
>>>class A(object):
>>> def __init__(self, aTag):
>>> self.__aTag = aTag
>>> self.__aList = []
>>
>> IMO, your problem starts right here. Not only are you using customized
>> attributes for each class, you're using class-private identifiers. You
>> would vastly simplify your work if you switch to single-underscore
>> attributes.
>
> I intentionally chose the class-private identifiers in my artificial
> example to emphasize that I was looking for a solution in which
> class B did not have to rely on particular knowledge of class A's
> implementation. That said, switching to single-underscores does not
> address the issue raised in the original post.
Not directly, but it does simplify possible solutions. For example, you
could use a straightforward getattr() approach where the class contains
an attribute listing all the deep-copyable attributes. You could even
use name-munging so that attributes can have an accompanying
ATTR_deepcopy() method for custom code so that your main __deepcopy__
method stays the same in subclasses.
(Obviously, this trick does in fact still work if you use private
attributes and do the name-mangling yourself, but I find that distasteful
for production code unless it's absolutely required.)
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"Given that C++ has pointers and typecasts, it's really hard to have a
serious conversation about type safety with a C++ programmer and keep a
straight face. It's kind of like having a guy who juggles chainsaws
wearing body armor arguing with a guy who juggles rubber chickens wearing
a T-shirt about who's in more danger." --Roy Smith, c.l.py, 2004.05.23
More information about the Python-list
mailing list