Polymorphic copy: Difference between revisions
Content added Content deleted
m (C++ is now a valid page name) |
(→{{header|Python}}: Improve example) |
||
Line 182: | Line 182: | ||
class T: |
class T: |
||
def classname(self): |
|||
return self.__class__.__name__ |
|||
def __init__(self): |
|||
⚫ | |||
self.myValue = "I'm a T." |
|||
def speak(self): |
|||
⚫ | |||
print self.classname(), 'Hello', self.myValue |
|||
⚫ | |||
print copy.copy(obj1).name() # prints "T" |
|||
print copy.copy(obj2).name() # prints "S"</python> |
|||
def clone(self): |
|||
⚫ | |||
return copy.copy(self) |
|||
⚫ | |||
def speak(self): |
|||
print self.classname(),"Meow", self.myValue |
|||
class S2(T): |
|||
def speak(self): |
|||
print self.classname(),"Woof", self.myValue |
|||
print "creating initial objects of types S1, S2, and T" |
|||
⚫ | |||
a.myValue = 'Green' |
|||
a.speak() |
|||
b = S2() |
|||
b.myValue = 'Blue' |
|||
b.speak() |
|||
⚫ | |||
u.myValue = 'Purple' |
|||
u.speak() |
|||
print "Making copy of a as u, colors and types should match" |
|||
u = a.clone() |
|||
u.speak() |
|||
a.speak() |
|||
print "Assigning new color to u, A's color should be unchanged." |
|||
u.myValue = "Orange" |
|||
u.speak() |
|||
a.speak() |
|||
print "Assigning u to reference same object as b, colors and types should match" |
|||
u = b |
|||
u.speak() |
|||
b.speak() |
|||
print "Assigning new color to u. Since u,b references same object b's color changes as well" |
|||
u.myValue = "Yellow" |
|||
u.speak() |
|||
b.speak()</python>Output of the above program is as follows<pre> |
|||
creating initial objects of types S1, S2, and T |
|||
S1 Meow Green |
|||
S2 Woof Blue |
|||
T Hello Purple |
|||
Making copy of a as u, colors and types should match |
|||
S1 Meow Green |
|||
S1 Meow Green |
|||
Assigning new color to u, A's color should be unchanged. |
|||
S1 Meow Orange |
|||
S1 Meow Green |
|||
Assigning u to reference same object as b, colors and types should match |
|||
S2 Woof Blue |
|||
S2 Woof Blue |
|||
Assigning new color to u. Since u,b references same object b's color changes as well |
|||
S2 Woof Yellow |
|||
S2 Woof Yellow</pre> |
|||
⚫ | The foregoing example uses the Python standard library ''copy'' module. The task, as stated,does not provide insight as to what should happen should the object contain lists of other objects. It could be necessary to use "deep copy" instead of copy. (using ''copy.deepcopy''). The distinction is important for complex objects containing references to other objects (for instances lists, tuples or dictionaries containing other lists, tuples or dictionaries as elements). The described task, as presented, offers no guidance on this matter. |
||
In many cases the most portable and robust "copy" would be made by serializing the source object and then de-serializing it back into the target. Under Python this would best be done with the ''pickle'' or ''cPickle'' |
In many cases the most portable and robust "copy" would be made by serializing the source object and then de-serializing it back into the target. Under Python this would best be done with the ''pickle'' or ''cPickle'' |