Object Reference question

Discussion in 'Python' started by josef, Aug 21, 2009.

  1. josef

    josef Guest

    To begin, I'm new with python. I've read a few discussions about
    object references and I think I understand them.

    To be clear, Python uses a "Pass By Object Reference" model.
    x = 1
    x becomes the object reference, while an object is created with the
    type 'int', value 1, and identifier (id(x)). 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.

    x in both cases is the object reference. I would like to use the
    object to refer to the object reference. If I have a gross
    misunderstanding, please correct me.

    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. Entering dk gives me the object: [MyClass0
    instance at 0x0000, MyClass1 instance at 0x0008, MyClass2 instance at
    0x0010 ... ]

    I need the object reference name (a,b,c,d) from dk to use as input for
    a file. Where do I find the memory location of the object reference
    and the object reference name memory location? I am unconcerned with
    the fact that the memory location will change the next time I run a
    python session. I will be using the object reference name for
    processing right away.

    My main focus of this post is: "How do I find and use object reference
    memory locations?"

    Thoughts?
    Thanks,

    Josef
     
    josef, Aug 21, 2009
    #1
    1. Advertising

  2. On Aug 20, 2009, at 11:07 PM, josef wrote:

    > To begin, I'm new with python. I've read a few discussions about
    > object references and I think I understand them.
    >
    > To be clear, Python uses a "Pass By Object Reference" model.
    > x = 1
    > x becomes the object reference, while an object is created with the
    > type 'int', value 1, and identifier (id(x)). 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.
    >
    > x in both cases is the object reference. I would like to use the
    > object to refer to the object reference.


    Stop right there. 'x' is not *the* object reference. It is *an*
    object reference (or in my preferred terminology, a label). Suppose
    you do:

    x = myclass()
    y = x

    The labels 'x' and 'y' both refer to the same object with equal
    precedence. There is no mapping from object back to label; it is a
    one-way pointer. Also importantly, labels themselves are not objects,
    and cannot be accessed or referred to.

    (This is a slight oversimplification; thanks to Python's reflection
    and introspection capabilities, it is possible to access labels to
    some extent, and in some limited situations it is possible to use
    stack inspection to obtain a label for an object. But this is hackish
    and error-prone, and should never be used when a more Pythonic method
    is available.)

    > 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. Entering dk gives me the object: [MyClass0
    > instance at 0x0000, MyClass1 instance at 0x0008, MyClass2 instance at
    > 0x0010 ... ]
    >
    > I need the object reference name (a,b,c,d) from dk to use as input for
    > a file.


    It sounds like you should either be storing that name as an attribute
    of the object, or using a dictionary ({'a': a, 'b': b, ...}).

    -Miles
     
    Miles Kaufmann, Aug 21, 2009
    #2
    1. Advertising

  3. josef

    Chris Rebert Guest

    On Thu, Aug 20, 2009 at 11:34 PM, Miles Kaufmann<> wrote:
    > On Aug 20, 2009, at 11:07 PM, josef wrote:

    <snip>
    >> 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. Entering dk gives me the object: [MyClass0
    >> instance at 0x0000, MyClass1 instance at 0x0008, MyClass2 instance at
    >> 0x0010 ... ]
    >>
    >> I need the object reference name (a,b,c,d) from dk to use as input for
    >> a file.

    >
    > It sounds like you should either be storing that name as an attribute of the
    > object, or using a dictionary ({'a': a, 'b': b, ...}).


    Shorter way to produce the same dictionary:
    dict(a=a, b=b, ...)

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Aug 21, 2009
    #3
  4. On Friday 21 August 2009 08:07:18 josef wrote:

    > My main focus of this post is: "How do I find and use object reference
    > memory locations?"


    >>> a = [1,2,3,4]
    >>> id(a)

    8347088
    >>>


    - Hendrik
     
    Hendrik van Rooyen, Aug 21, 2009
    #4
  5. josef

    josef Guest

    On Aug 21, 1:34 am, Miles Kaufmann <> wrote:
    > On Aug 20, 2009, at 11:07 PM, josef wrote:
    >
    > > To begin, I'm new with python. I've read a few discussions about
    > > object references and I think I understand them.

    >
    > > To be clear, Python uses a "Pass By Object Reference" model.
    > > x = 1
    > > x becomes the object reference, while an object is created with the
    > > type 'int', value 1, and identifier (id(x)). 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.

    >
    > > x in both cases is the object reference. I would like to use the
    > > object to refer to the object reference.

    >
    > Stop right there.  'x' is not *the* object reference.  It is *an*  
    > object reference (or in my preferred terminology, a label).  Suppose  
    > you do:
    >
    > x = myclass()
    > y = x


    It would not make sense to do that in the context of the software I am
    writing. The documentation will specifically state not to do that. If
    the user does do that, then the user will be disappointed and possibly
    angry.

    >
    > The labels 'x' and 'y' both refer to the same object with equal  
    > precedence.  There is no mapping from object back to label; it is a  
    > one-way pointer.  Also importantly, labels themselves are not objects,  
    > and cannot be accessed or referred to.


    I would just like to store the name of the one way pointer.

    >
    > (This is a slight oversimplification; thanks to Python's reflection  
    > and introspection capabilities, it is possible to access labels to  
    > some extent, and in some limited situations it is possible to use  
    > stack inspection to obtain a label for an object.  But this is hackish  
    > and error-prone, and should never be used when a more Pythonic method  
    > is available.)


    Hackish is fine. How error-prone is this method?
    >
    > > 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. Entering dk gives me the object: [MyClass0
    > > instance at 0x0000, MyClass1 instance at 0x0008, MyClass2 instance at
    > > 0x0010 ... ]

    >
    > > I need the object reference name (a,b,c,d) from dk to use as input for
    > > a file.

    >
    > It sounds like you should either be storing that name as an attribute  
    > of the object, or using a dictionary ({'a': a, 'b': b, ...}).


    That solution was mentioned in some of the discussions I read, but I
    would like to stay away from something like: a = MyClass
    (name='a', ...). Is it possible to assign an object reference name in
    a class __init__ defintion?
     
    josef, Aug 21, 2009
    #5
  6. josef

    Dave Angel Guest

    josef wrote:
    > To begin, I'm new with python. I've read a few discussions about
    > object references and I think I understand them.
    >
    > To be clear, Python uses a "Pass By Object Reference" model.
    > x = 1
    > x becomes the object reference, while an object is created with the
    > type 'int', value 1, and identifier (id(x)). 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.
    >
    > x in both cases is the object reference. I would like to use the
    > object to refer to the object reference. If I have a gross
    > misunderstanding, please correct me.
    >
    > 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. Entering dk gives me the object: [MyClass0
    > instance at 0x0000, MyClass1 instance at 0x0008, MyClass2 instance at
    > 0x0010 ... ]
    >
    > I need the object reference name (a,b,c,d) from dk to use as input for
    > a file. Where do I find the memory location of the object reference
    > and the object reference name memory location? I am unconcerned with
    > the fact that the memory location will change the next time I run a
    > python session. I will be using the object reference name for
    > processing right away.
    >
    > My main focus of this post is: "How do I find and use object reference
    > memory locations?"
    >
    > Thoughts?
    > Thanks,
    >
    > Josef
    >
    >

    There was a similar query here within the last couple of months, and
    lots of interesting discussion. But I never saw a use case convincing
    enough for me to want to remember how the various suggestions worked.
    Just how are you planning to use this? Are you planning to write a
    debugger?

    Or are you trying to keep mnemonic names for all instances of a
    particular class? Is this for a particular program's use, or are you
    trying to create a library to be used to reverse engineer some software
    you con't control?

    Several of your phrasings imply you don't understand Python yet.

    "memory location" - invisible to python use. And although id() will
    give you a hash-code that's actually a memory address, there's no direct
    way to use it. And names (attributes) don't necessarily have an address.
    "the object reference name (a,b,c,d) from dk" What is this? There's
    nothing that even conceptually looks like that when you assign dk =
    [a, b, c, d]


    A given object may have one to many references, and some of these may
    have names. If you constrain those names to be in a particular context,
    it may be possible to search for which name(s) currently happen(s) to
    point to the given object. For example, if you have the following at
    top level in a module:

    a = MyClass0()
    b = MyClass1()
    c = MyClass2()
    dk = [a, b, c]

    then, given the id() of dk[2], you could search the particular modules
    global name dictionary, and find c. But for the following fragment, you
    could not:

    a = MyClass0()
    b = MyClass1()
    c = MyClass2()
    dk = [a, b, c]
    c = 42

    dk remains the same, but the dk[2] item no longer has any name
    referencing it.

    At any given instant of time, most objects in a typical program have no
    name associated with them. Many of them never did have a name. What
    would you want if dk had been created as:

    dk = [MyClass0(), MyClass1(), MyClass2()]

    or if a, b, and/or c were local variables in a function that's long
    since quit running, or that has run many times, each time creating new
    objects?

    DaveA
     
    Dave Angel, Aug 21, 2009
    #6
  7. josef a écrit :
    > To begin, I'm new with python. I've read a few discussions about
    > object references and I think I understand them.
    >
    > To be clear, Python uses a "Pass By Object Reference" model.
    > x = 1
    > x becomes the object reference, while an object is created with the
    > type 'int', value 1, and identifier (id(x)). 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.
    >
    > x in both cases is the object reference.


    Nope. It's *a* reference to the object - or, more exactly, a key in a
    mapping (the current namespace), which is associatied with a reference
    to the object. You can translate:

    x = 1

    to:

    current_namespace['x'] = int(1)


    > I would like to use the
    > object to refer to the object reference. If I have a gross
    > misunderstanding, please correct me.
    >
    > 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. Entering dk gives me the object: [MyClass0
    > instance at 0x0000, MyClass1 instance at 0x0008, MyClass2 instance at
    > 0x0010 ... ]
    >
    > I need the object reference name (a,b,c,d) from dk to use as input for
    > a file.


    ???

    Could you elaborate, please ?

    > Where do I find the memory location of the object reference
    > and the object reference name memory location?


    short answer : you don't. Python is a high level language, 'memory
    location' is an implementation detail (and highly
    implementation-dependant), and *not* exposed (at least not in any usable
    way).

    > I am unconcerned with
    > the fact that the memory location will change the next time I run a
    > python session. I will be using the object reference name for
    > processing right away.
    >
    > My main focus of this post is: "How do I find and use object reference
    > memory locations?"
    >
    > Thoughts?


    Yes : please explain the problem you're trying to solve. I mean, the
    *real* problem - what you want to achieve -, not what you think is the
    solution !-)
     
    Bruno Desthuilliers, Aug 21, 2009
    #7
  8. josef

    Simon Forman Guest

    On Aug 21, 2:07 am, josef <> wrote:
    > To begin, I'm new with python. I've read a few discussions about
    > object references and I think I understand them.
    >
    > To be clear, Python uses a "Pass By Object Reference" model.
    > x = 1
    > x becomes the object reference, while an object is created with the
    > type 'int', value 1, and identifier (id(x)). 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.
    >
    > x in both cases is the object reference. I would like to use the
    > object to refer to the object reference. If I have a gross
    > misunderstanding, please correct me.


    x is not the object reference, it's just a string used as a key in a
    dict (the value is the object reference):

    |>>> globals()
    |{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
    '__main__', '__doc__': None}
    |>>> x = 1
    |>>> globals()
    |{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
    '__main__', '__doc__': None, 'x': 1}


    I don't understand what you're trying to do, but give up the idea of
    "object reference memory locations". Any name binding in python just
    associates a string with a object in one of various namespace
    dictionaries.

    HTH,
    ~Simon
     
    Simon Forman, Aug 21, 2009
    #8
  9. josef

    josef Guest

    On Aug 21, 4:26 am, Ben Finney <> wrote:
    > josef <> 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
     
    josef, Aug 21, 2009
    #9
  10. josef

    Simon Forman Guest

    On Aug 21, 12:12 pm, josef <> wrote:
    > > > 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?


    There's no such thing as "object reference names". Object references
    do not have names.

    When you run a piece of code like "a = object()" a string "a" is used
    as a key in a dictionary and the value is the "object reference".

    Consider:

    [None][0] = object()

    (Create an anonymous list object with one item in it and immediately
    replace that item with an anonymous object instance.)

    There's no name to catch.

    Objects know nothing about their "enclosing" namespace. So if you want
    them to have names you have to explicitly give them name attributes.
    (However, this won't affect their enclosing namespaces.)

    As Bruno said, think of "foo = something" as shorthand for
    "some_namespace_dictionary['foo'] = something".


    The two exceptions to this are 'def' and 'class' statements which both
    create name->object (key, value) pairs in their current namespace AND
    give the objects they create '__name__' attributes (pretty much just
    as an aid to debugging.)

    In [1]: def foo(): pass
    ...:

    In [2]: foo.__name__
    Out[2]: 'foo'

    In [3]: class foo(): pass
    ...:

    In [4]: foo.__name__
    Out[4]: 'foo'


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


    That's the way to do it. :]
     
    Simon Forman, Aug 21, 2009
    #10
  11. josef a écrit :

    (snip)
    > 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?


    In Python, classes and functions are objects too. The class and def
    statements are mostly syntactic sugar that *both* instanciate the (resp)
    class of function objet *and* bind it in the local namespace. FWIW,
    while class and function objects do (usually) have a __name__ attribute
    (usually the one used in the class or def statement...), this doesn't
    mean they're still bound to this name, nor that they are not bound to
    any other name:

    bruno@bruno:~$ python
    Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    [GCC 4.3.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    pythonrc start
    pythonrc done
    >>> def foo(): print "function foo ???"

    ....
    >>> foo

    <function foo at 0x8563e9c>
    >>> foo.__name__

    'foo'
    >>> bar = foo
    >>> bar.__name__

    'foo'
    >>> bar()

    function foo ???
    >>> import sys
    >>> foo = lambda: sys.stdout.write("pick a boo !\n")
    >>> foo.__name__

    '<lambda>'
    >>> foo()

    pick a boo !
    >>> funcs = [foo, bar]
    >>> del foo
    >>> funcs[0]

    <function <lambda> at 0x8563ed4>
    >>> funcs[0]()

    pick a boo !
    >>> funcs[1]

    <function foo at 0x8563e9c>
    >>> funcs[1]()

    function foo ???
    >>> funcs[1].__name__

    'foo'
    >>> del bar
    >>> funcs[1]()

    function foo ???
    >>> funcs[1].__name__ = "yadda"
    >>> funcs[1]()

    function foo ???
    >>> funcs[1].__name__

    'yadda'
    >>>


    HTH
     
    Bruno Desthuilliers, Aug 24, 2009
    #11
  12. josef

    Aahz Guest

    In article <>,
    Hendrik van Rooyen <> wrote:
    >On Friday 21 August 2009 08:07:18 josef wrote:
    >>
    >> My main focus of this post is: "How do I find and use object reference
    >> memory locations?"

    >
    >>>> a = [1,2,3,4]
    >>>> id(a)

    >8347088
    >>>>


    Of course, that doesn't actually allow you to do anything...
    --
    Aahz () <*> http://www.pythoncraft.com/

    "I support family values -- Addams family values" --www.nancybuttons.com
     
    Aahz, Aug 25, 2009
    #12
  13. On Tuesday 25 August 2009 21:32:09 Aahz wrote:
    > In article <>,
    >
    > Hendrik van Rooyen <> wrote:
    > >On Friday 21 August 2009 08:07:18 josef wrote:
    > >> My main focus of this post is: "How do I find and use object reference
    > >> memory locations?"
    > >>
    > >>>> a = [1,2,3,4]
    > >>>> id(a)

    > >
    > >8347088

    >
    > Of course, that doesn't actually allow you to do anything...


    Well - if the OP is the sort of person who likes juggling with running
    chainsaws, then he can look up a thread I started about a thing I called
    a "can", which enabled you to get the object back from a string
    representation of the ID. I did not want to open that "can" of worms again,
    and I thought that answering half a question was better than nothing...

    *weg* - Hendrik
     
    Hendrik van Rooyen, Aug 26, 2009
    #13
  14. josef

    josef Guest

    Thanks to everyone who responded.

    I will be going with some sort of a = MyClass(name = 'a') format. It's
    the Python way.

    For me, it was very hard to accept that EVERYTHING is an object
    reference. And that there are no object reference names, just string
    entries in dictionaries. But I think it all makes sense now.

    Thanks again,

    Josef


    On Aug 21, 1:07 am, josef <> wrote:
    > To begin, I'm new with python. I've read a few discussions about
    > object references and I think I understand them.
    >
    > To be clear, Python uses a "Pass By Object Reference" model.
    > x = 1
    > x becomes the object reference, while an object is created with the
    > type 'int', value 1, and identifier (id(x)). 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.
    >
    > x in both cases is the object reference. I would like to use the
    > object to refer to the object reference. If I have a gross
    > misunderstanding, please correct me.
    >
    > 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. Entering dk gives me the object: [MyClass0
    > instance at 0x0000, MyClass1 instance at 0x0008, MyClass2 instance at
    > 0x0010 ... ]
    >
    > I need the object reference name (a,b,c,d) from dk to use as input for
    > a file. Where do I find the memory location of the object reference
    > and the object reference name memory location? I am unconcerned with
    > the fact that the memory location will change the next time I run a
    > python session. I will be using the object reference name for
    > processing right away.
    >
    > My main focus of this post is: "How do I find and use object reference
    > memory locations?"
    >
    > Thoughts?
    > Thanks,
    >
    > Josef
     
    josef, Aug 27, 2009
    #14
  15. josef

    Ethan Furman Guest

    josef wrote:
    > Thanks to everyone who responded.
    >
    > I will be going with some sort of a = MyClass(name = 'a') format. It's
    > the Python way.
    >
    > For me, it was very hard to accept that EVERYTHING is an object
    > reference. And that there are no object reference names, just string
    > entries in dictionaries. But I think it all makes sense now.
    >
    > Thanks again,
    >
    > Josef


    My apologies if I missed it, but what *exactly* are you planning on
    doing with your 'name' attribute? From the posts I've seen so far, I
    think you are only setting yourself up for failure.

    ~Ethan~
     
    Ethan Furman, Aug 27, 2009
    #15
  16. josef

    josef Guest

    On Aug 27, 1:35 pm, Ethan Furman <> wrote:
    > josef wrote:
    > > Thanks to everyone who responded.

    >
    > > I will be going with some sort of a = MyClass(name = 'a') format. It's
    > > the Python way.

    >
    > > For me, it was very hard to accept that EVERYTHING is an object
    > > reference. And that there are no object reference names, just string
    > > entries in dictionaries. But I think it all makes sense now.

    >
    > > Thanks again,

    >
    > > Josef

    >
    > My apologies if I missed it, but what *exactly* are you planning on
    > doing with your 'name' attribute?  From the posts I've seen so far, I
    > think you are only setting yourself up for failure.
    >
    > ~Ethan~


    I'm going to use it for printing purposes. dk = MyClass(name='dk')
    When I need a name dk.name. There will only ever be one dk defined.

    Does that read like I'm setting myself up for failure?
     
    josef, Aug 28, 2009
    #16
  17. josef

    Ethan Furman Guest

    josef wrote:
    > On Aug 27, 1:35 pm, Ethan Furman <> wrote:
    >
    >>josef wrote:
    >>
    >>>Thanks to everyone who responded.

    >>
    >>>I will be going with some sort of a = MyClass(name = 'a') format. It's
    >>>the Python way.

    >>
    >>>For me, it was very hard to accept that EVERYTHING is an object
    >>>reference. And that there are no object reference names, just string
    >>>entries in dictionaries. But I think it all makes sense now.

    >>
    >>>Thanks again,

    >>
    >>>Josef

    >>
    >>My apologies if I missed it, but what *exactly* are you planning on
    >>doing with your 'name' attribute? From the posts I've seen so far, I
    >>think you are only setting yourself up for failure.
    >>
    >>~Ethan~

    >
    >
    > I'm going to use it for printing purposes. dk = MyClass(name='dk')
    > When I need a name dk.name. There will only ever be one dk defined.
    >
    > Does that read like I'm setting myself up for failure?


    I was hoping someone with more expertise than myself would answer that.
    :) Oh well.

    The best answer I can give is that you do not want to use 'name' to
    reference the object itself, but only for printing/debugging purposes.
    'name' is just a label for your object, and not necessarily the only
    label; that particular label may also be lost... Consider:

    In [5]: class MyClass(object):
    ...: def __init__(self, name):
    ...: self.name = name
    ...: def __repr__(self):
    ...: return "MyClass(name='%s')" % self.name
    ...:

    In [6]: dk = MyClass(name='dk')

    In [7]: dk
    Out[7]: MyClass(name='dk')

    In [8]: se = dk

    In [9]: del dk

    In [10]: se
    Out[10]: MyClass(name='dk')

    In [11]: dk
    -------------------------------------------------------------------
    NameError Traceback (most recent call last)

    C:\pythonlib\<ipython console> in <module>()

    NameError: name 'dk' is not defined

    As you can see, just because you have saved the original name does not
    gaurantee that same name will always reference that same object, or any
    object.

    Hope this helps!

    ~Ethan~
     
    Ethan Furman, Aug 31, 2009
    #17
  18. Ethan Furman a écrit :
    (snip)

    > The best answer I can give is that you do not want to use 'name' to
    > reference the object itself, but only for printing/debugging purposes.


    Which is what the OP stated !-)

    > 'name' is just a label for your object, and not necessarily the only
    > label; that particular label may also be lost... Consider:


    (snip example)

    > As you can see, just because you have saved the original name does not
    > gaurantee that same name will always reference that same object, or any
    > object.



    FWIW, the __name__ attributes of functions, classes and modules works
    just the same...
     
    Bruno Desthuilliers, Sep 1, 2009
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFubnkgQ2hvaGFu?=

    Question: Object Reference not set to an instance of an object.

    =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,225
    Kevin Spencer
    Jan 3, 2005
  2. ce
    Replies:
    1
    Views:
    3,019
  3. Replies:
    1
    Views:
    515
    Mark Rae [MVP]
    Sep 20, 2007
  4. Arv
    Replies:
    15
    Views:
    952
    James Kanze
    Mar 7, 2008
  5. Eric Mahurin
    Replies:
    4
    Views:
    259
    David A. Black
    May 6, 2005
Loading...

Share This Page