Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
copying an object - copy the reference

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

Related Material

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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/python-c ... rence.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard