Re: Copying objects and multiple inheritance



En Tue, 02 Jun 2009 19:02:47 -0300, Brian Allen Vanderburg II <BrianVanderburg2@xxxxxxx> escribió:

What is the best way to copy an object that has multiple inheritance with the copy module. Particularly, some of the instances in the hierarchy

("...some of the classes in...", I presume?)

use the __copy__ method to create a copy (because even for shallow copies they need some information updated a little differently), so how can I make sure all the information is copied as it is supposed to be even for the base classes that have special requirements.

If you don't control all the clases involved, there is little hope for a method like __copy__ to work at all... All classes must be written with cooperation in mind, using super() the "right" way. See "Python's Super Considered Harmful" [1] and "Things to Know About Python Super" [2][3][4]

That said, and since multiple inheritance is the heart of the problem, maybe you can redesign your solution *without* using MI? Perhaps using delegation instead?

[1] http://fuhm.net/super-harmful/
[2] http://www.artima.com/weblogs/viewpost.jsp?thread=236275
[3] http://www.artima.com/weblogs/viewpost.jsp?thread=236278
[4] http://www.artima.com/weblogs/viewpost.jsp?thread=237121

--
Gabriel Genellina

.



Relevant Pages

  • =?windows-1252?Q?Re=3A_Things_to_know_about_=91super=92_=28was=3A_super=28=29_a?= =?windows-
    ...     use super consistently, and document that you use it, as it is part ... (this is a literal citation from "super considered harmful" by James ... away multiple inheritance, it is just not worth the complication. ... this is how a developer can document his hierarchy ...
    (comp.lang.python)
  • Re: interesting article on quantum cryptography
    ... pig was empty, super, and tempts except for the hierarchy. ... remain often than cook with Zakariya's ethical habitat. ...
    (sci.crypt)
  • super(...).__init__() vs Base.__init__(self)
    ... The super() method only works correctly in multiple inheritance when the base classes are written to expect it, so "Always use super" seems like bad advice. ... Another fix is for Base.__init__to call super.__init__and to list Base first in the list of base classes. ... This might break existing code that is written in the style of fix 1 (calling both base class __init__() methods explicitly). ...
    (comp.lang.python)
  • Re: Call-Super antipattern
    ... As you are having a good look at that class and therefore know if a method needs to call super or not to keep a consistent state for itself. ... I look at it as making each class in the hierarchy responsible for making sure that their method can be called by subclasses. ... Perhaps by introducing a keyword in the declaration, and requiring that it be in any 'overriding' declaration as well, so if a programmer didn't realise, and omitted it, the compiler would tell them. ... public class Vehicle ...
    (comp.lang.java.programmer)
  • Re: super() doesnt get superclass
    ... Why are you looking for the superclass of A? ... classis/are the superclass" is an implementation detail that you ... super is sadly confusing and hard to use. ... provided all the base classes *and their bases classes* themselves also ...
    (comp.lang.python)