Finding the instance reference of an object [long and probably boring]

Aaron Brady castironpi at gmail.com
Fri Nov 7 16:27:10 EST 2008


On Nov 7, 3:03 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> Joe Strout <j... at strout.net> writes:
> > So.  How about this for a summary?
>
> > "Python uses call-by-sharing.  That's a special case of call-by-value
> > where the variables are references to objects; it is these references
> > that are copied to the parameters, not the objects themselves.  For
> > users of other languages, this is the same semantics used for objects
> > in Java, RB/VB.NET, and C++ when dealing with objects."
>
> Here's a story about call by sharing:
>
> One day a black cat came strolling into our garden.  It seemed quite
> hungry so we gave it some milk and food remains.  The cat drank the milk
> and ate the food, stayed for a bit then walked away.  A couple of days
> later it came back, miaowing, so we fed it again.  It started coming to
> see us almost every day, sometimes sleeping in the house, sometimes
> disappearing for a day or two.  We started thinking of it as our cat and
> we *named* it Napoleon.  Napoleon became very popular and stayed at home
> more and more often, and very young son was very fond of it, calling it
> something like 'Polion'.
>
> Unfortunately Napoleon got infested with fleas and we had to take him to
> the vet.  The vet scanned Napoleon with a little machine and discovered
> that it had an ID chip.  This revealed that Napoleon was really called
> Nelson (ironically!) and belonged to a house down our road.  We
> contacted them and they were happy to 'share' the cat with us.  By this
> time the cat answered to the name of Napoleon so we carried on calling
> it this name.
>
> This story ends quite sadly.  One day Napoleon escaped out the front
> door and got run over by a passing van.  Our son kept asking for Polion
> so we decided to get a new cat and called him Napoleon as well (we
> decided it would be easier for him!).
>
> Now some questions about the story:
Sorry didn't read above yet.

> 1. Is Napoleon a copy of Dobby or are they the same cat?
Same cat.

> 2. Is Polion a copy of Napoleon or are they the same cat?
Same cat.

> 3. When we got rid of Napoleon's fleas, was Nelson deflea-ed as well?
Yes.

> 4. When Napoleon died, did Nelson die as well?
No, honey.  He lives on in all of us.

> 5. When we got a new Napoleon, does this mean that our neighbours got a
>    new Nelson?
No, darling, Nelson is just sleeping.  When we got a New Orleans,
where did the old one go?

> Now a question about the questions about the story:
>
>    To be able to understand the story and answer questions 1-5, do we
>    need to think of Napoleon, Nelson and Polion as variables containing
>    references to cat objects, or is it enough to think of them as three
>    names for cats?

I think they are names, implying that Python has a different "variable
model" than C++.

However, a[0] isn't exactly a name, per se, and if you say that 'b'
and 'a[0]' are names of an object, then 'a[1-1]', 'a[2*0]', etc. are
all names of it.  Furthermore, some class models variables like this:

a.b= 'abc'
a.c= 'def'
a.d= 'ghi'

It also allows index access: a[0], a[1], a[2], respectively.  'abc'
has two names: 'a.b', and 'a[0]'.  Correct?




More information about the Python-list mailing list