I call my father "Dad" but everyone else calls him "Norman". Same person, different name.
If Dad puts on the hat that he's got from the Christmas cracker before he eats his Christmas pudding, then everyone else sees that Norman has put on his hat before eating his Christmas pudding. Two names for one and the same person.
PYTHON - COPY BY REFERENCE
The same principle applies to the copying of objects in Python (and Java and PHP 5 and other object oriented languages); an assignment statement is just the addition of an extra name to the same object.
Let's see an example - in Python:
pie = ["apple","pear","strawberry"]
pudding = pie
pudding[1] = "apricot"
print pudding
print pie
When I run that:
earth-wind-and-fire:~/feb05 grahamellis$ python obcop.py
['apple', 'apricot', 'strawberry']
['apple', 'apricot', 'strawberry']
earth-wind-and-fire:~/feb05 grahamellis$
By changing "pudding" I've also changed "pie".
PERL - COPY BY CONTENTS
In contrast, if I copy a list in Perl (and in PHP 4, Tcl and other languages which are not 100% object oriented), I duplicate the data. Example in Perl:
@pie = ("apple","pear","strawberry");
@pudding = @pie;
$pudding[1] = "apricot";
print "@pudding\n";
print "@pie\n";
Which runs:
earth-wind-and-fire:~/feb05 grahamellis$ perl obcop.pl
apple apricot strawberry
apple pear strawberry
earth-wind-and-fire:~/feb05 grahamellis$
See also
Python programming course
Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via
our main web site
Python - More on Collections and Sequences [61] - ()
[386] - ()
[633] - ()
[899] - ()
[1304] - ()
[1310] - ()
[1869] - ()
[1873] - ()
[2718] - ()
[2894] - ()
[2920] - ()
[2996] - ()
[3150] - ()
[3348] - ()
[3439] - ()
[3797] - ()
[4398] - ()
[4442] - ()
Object Orientation and General technical topics - Programming Principles [2001] - ()
[2022] - ()
[2228] - ()
[2310] - ()
[2327] - ()
[2415] - ()
[2510] - ()
[2550] - ()
[2586] - ()
[2737] - ()
[2769] - ()
[2878] - ()
[2915] - ()
[2964] - ()
[3026] - ()
[3456] - ()
[3542] - ()
[3548] - ()
[3551] - ()
[3673] - ()
[3878] - ()
[3928] - ()
[3954] - ()
[4003] - ()
[4061] - ()
[4090] - ()
[4118] - ()
[4153] - ()
[4206] - ()
[4325] - ()
[4611] - ()
[4632] - ()
[4645] - ()
Object Oriented Python [477] - ()
[834] - ()
[900] - ()
[1306] - ()
[1348] - ()
[1925] - ()
[2017] - ()
[2169] - ()
[2604] - ()
[3085] - ()
[3399] - ()
[3436] - ()
[3673] - ()
[3878] - ()
[3947] - ()
[4021] - ()
[4028] - ()
[4129] - ()
[4448] - ()
[4591] - ()
[4650] - ()
[4721] - ()
resource index - Python
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth and
delegate's questions answered at
the
Opentalk forum.
At Well House Consultants, we provide
training courses on
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.