Polymorphic copy: Difference between revisions

→‎{{header|Python}}: Improve example
m (C++ is now a valid page name)
(→‎{{header|Python}}: Improve example)
Line 182:
 
class T:
def nameclassname(self): return "T"
return self.__class__.__name__
 
def __init__(self):
class S:
def name( self):.myValue return= "SI'm a T."
 
def speak(self):
obj1 = T()
print self.classname(), 'Hello', self.myValue
obj2 = S()
print copy.copy(obj1).name() # prints "T"
print copy.copy(obj2).name() # prints "S"</python>
 
def clone(self):
Note the forgoing example uses the Python standard library ''copy'' module. However, it gives no insight into what is happening under the hood. Also it takes no consideration of whether a "deep copy" is necessary (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.
return copy.copy(self)
 
class SS1(T):
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"
obj2a = SS1()
a.myValue = 'Green'
a.speak()
 
b = S2()
b.myValue = 'Blue'
b.speak()
 
obj1u = T()
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>
 
NoteThe the forgoingforegoing example uses the Python standard library ''copy'' module. HoweverThe task, itas givesstated,does nonot provide insight intoas to what isshould happeninghappen undershould the hood.object contain Alsolists itof takesother noobjects. consideration ofIt could be necessary whetherto ause "deep copy" isinstead necessaryof 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''
Anonymous user