Object Reference question

Ben Finney ben+python at benfinney.id.au
Fri Aug 21 05:26:54 EDT 2009


josef <josefg 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 :-)

> 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.

> 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.

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

> 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



More information about the Python-list mailing list