Object Reference question

josef josefg at gmail.com
Fri Aug 21 12:12:36 EDT 2009


On Aug 21, 4:26 am, Ben Finney <ben+pyt... at benfinney.id.au> wrote:
> josef <jos... at gmail.com> writes:
> > To be clear, Python uses a "Pass By Object Reference" model.
>
> Yes. (I'm glad this concept has propagated to newcomers so well :-)

I found one really good discussion on python semantics versus other
languages. It gave me this gem of a quote:

"When I turn on the TV and see Chuck Norris, though, I know it's only
a reference to Chuck Norris, or I would be blinded.  The only case he
needs is "Pass By Roundhouse Kick"." -Chuckk

>
> > x = 1
> > x becomes the object reference
>
> It becomes *a* reference to that object, independent of any other
> references to that same object.
>
> > while an object is created with the type 'int', value 1, and
> > identifier (id(x)).
>
> Not really “while”. The object creation happens first, then the
> assignment statement binds a reference to that object.
>
> > Doing this with a class, x = myclass(), does the same thing, but with
> > more or less object attributes. Every object has a type and an
> > identifier (id()), according to the Python Language Reference for
> > 2.6.2 section 3.1.
>
> Any expression can be on the right side of the assignment operator. The
> expression will evaluate to some object, which the assignment will then
> bind to the reference on the left side of the assignment operator.
>
> > x in both cases is the object reference.
>
> It is *an* object reference; that is, it's an identifier which refers to
> an object. There's nothing about that identifier that makes it “the (one
> and only) object reference”.
>
> > I would like to use the object to refer to the object reference. If I
> > have a gross misunderstanding, please correct me.
>
> Yes, it's a simple misunderstanding: objects do not, in general, know
> any of the references there may be to them.
>
> > The following is what I would like to do: I have a list of class
> > instances dk = [ a, b, c, d ], where a, b, c, d is an object
> > reference.
>
> Note that, after that list is created, each item in that list is *also*
> a reference to the corresponding object. That is, ‘a’ is a reference to
> an object, and ‘dk[0]’ is a *different* reference to the *same* object.
> The object has no knowledge about those references.

This is surprising. My initial thought is that dk[0] hold the object
reference 'a,' but that wouldn't be true "pass by object reference."
When defining the object reference dk[0], python takes the object
reference 'a,' finds the object MyClass0(), and then assigns the
object identity to dk[0]? Or something close to that.

>
> > Entering dk gives me the object: [MyClass0 instance at 0x0000,
> > MyClass1 instance at 0x0008, MyClass2 instance at 0x0010 ... ]
>
> This is a hint that, when asked for a string representation, each of the
> objects in that list can say little more than that they are of a
> particular type, and are located at a particular memory address. They do
> not know any of the references to themselves.
>
> > I need the object reference name (a,b,c,d) from dk to use as input for
> > a file.
>
> You'll have to track that yourself.

I'm a bit shocked that there isn't a method for catching object
reference names. I think that something like a = MyClass0(name =
'a', ...) is a bit redundant. Are definitions treated the same way?
How would one print or pass function names?

>
> A good way to keep track of name-to-object mappings is with Python's
> built-in mapping type, ‘dict’::
>
>     dk = {'a': a, 'b': b, 'c': c, 'd': d}
>
> (There are more efficient ways to create a dictionary without such
> repetition, of course, but this is more illustrative of the point.)
>
> You can then get a list (assembled in arbitrary sequence) of just the
> keys, or just the values, or the key-value pairs, from the dict with its
> ‘keys’, ‘values’, and ‘items’ methods respectively::
>
>     >>> dk = {'a': a, 'b': b, 'c': c, 'd': d}
>     >>> dk.keys()
>     ['a', 'c', 'd', 'b']
>     >>> dk.values()
>     [<MyClass instance at 0x3462>, <MyClass instance at 0x2983>, <MyClass instance at 0x3717>, <MyClass instance at 0x3384>]
>     >>> dk.items()
>     [('b', <MyClass instance at 0x2983>), ('c', <MyClass instance at 0x3462>), ('a',  <MyClass instance at 0x3384>), ('d',  <MyClass instance at 0x3717>)]
>
> Each of these is even better used as the iterable for a ‘for’ loop::
>
>     >>> for (key, value) in dk.items():
>     ...     print "Here is item named", key
>     ...     print value

I think I'll just add a 'name' to the classes' init defintion.

>
> > My main focus of this post is: "How do I find and use object reference
> > memory locations?"
>
> You don't. Use the references in your code, forget about the memory
> addresses, and remember that container objects themselves contain
> references, so use them for organising your objects.
>
> --
>  \     “Demagogue: One who preaches doctrines he knows to be untrue to |
>   `\                     men he knows to be idiots.” —Henry L. Mencken |
> _o__)                                                                  |
> Ben Finney

Thanks,

Josef



More information about the Python-list mailing list