Accessing an instance via its memory address (instance at ...)

Discussion in 'Python' started by Kent Johnson, Nov 12, 2004.

  1. Kent Johnson

    Kent Johnson Guest

    wrote:
    > Hi
    >
    > I have a list containing several instance address, for example:
    >
    > [<JavaClass instance at 00BAC290>, <JavaClass instance at 00BB0D10>,
    > <JavaClass instance at 00BA5230>]
    >
    > I'd like to invoke a method on each of these instance but I don't know :
    >
    > 1. if its possible

    Certainly

    > 2. how to proceed


    lst = ... # your list
    # Call method() for each instance in l
    for instance in lst:
    instance.method()

    # Call method() for a particular instance in lst
    lst[0].method()

    Kent
     
    Kent Johnson, Nov 12, 2004
    #1
    1. Advertising

  2. Kent Johnson

    Guest Guest

    Hi

    I have a list containing several instance address, for example:

    [<JavaClass instance at 00BAC290>, <JavaClass instance at 00BB0D10>,
    <JavaClass instance at 00BA5230>]

    I'd like to invoke a method on each of these instance but I don't know :

    1. if its possible
    2. how to proceed

    someone can help me ?

    tks

    simon
     
    Guest, Nov 12, 2004
    #2
    1. Advertising

  3. Kent Johnson

    Terry Reedy Guest

    <> wrote in message
    news:cn2fkq$k6u$...
    > I have a list containing several instance address, for example:


    I doubt that.

    > [<JavaClass instance at 00BAC290>, <JavaClass instance at 00BB0D10>,
    > <JavaClass instance at 00BA5230>]


    This is how Python prints a list of instance objects. If the list
    contained the integer ids, which would only happen if you explicitly
    created a list of ids (pretty useless, usually), it would print them as
    integers. Kent's solution to your questions assumes that you indeed have
    objects and not addresses.

    Terry J. Reedy
     
    Terry Reedy, Nov 12, 2004
    #3
  4. On Fri, 12 Nov 2004 14:59:54 +0100, <> wrote:

    >Hi
    >
    >I have a list containing several instance address, for example:
    >
    >[<JavaClass instance at 00BAC290>, <JavaClass instance at 00BB0D10>,
    ><JavaClass instance at 00BA5230>]

    ^^^^^^^^--[1]
    [1] Is that what you mean by instance address? While that is an actual
    hex representation of a memory address in some implementations, the purpose
    of the number is identification, not access. What you have in the list is
    actually a sequence of references to object representations. In python all
    objects are accessed via these references, which internally may be pointers
    or indices into storage or composite handles or whatever -- that is up to the
    implementation. But when you write source code selecting such a reference, as
    in my_list[2] selecting the 3rd (indexing from 0) reference in a list, that
    accomplishes access to the object, and you can then write a trailing expression
    like .foo() to call a foo method or .foo could be a non-method attribute, or
    your selected reference might be to another list, in which case you could
    add a trailing [123] to select from that list. Examples just mentioned
    spelled out:
    my_list[2].foo()
    my_list[2].foo
    my_list[2][123]

    >
    >I'd like to invoke a method on each of these instance but I don't know :
    >
    >1. if its possible
    >2. how to proceed
    >

    others have already demonstrated several ways, I just wanted to elaborate
    on the concept of references vs the objects they refer to. Note that assignment
    in python works with these object references too, so that when you write
    a = my_list[2]
    b = a
    you have bound both names to the same object that my_list[2] refers to.
    >someone can help me ?


    Example of above things:

    >>> class A(object):

    ... def foo(self): return '--> This is an instance of class A with id 0x%08X'%id(self)
    ...
    >>> my_list = [A() for dummy in range(2)]
    >>> my_list

    [<__main__.A object at 0x009011B0>, <__main__.A object at 0x009011F0>]

    That looks similar to your list of objects. We can select one of them:

    >>> my_list[1]

    <__main__.A object at 0x009011F0>

    And call the foo method by adding .foo() to the aforegoing expression:
    >>> my_list[1].foo()

    '--> This is an instance of class A with id 0x009011F0'

    Same for the other one:
    >>> my_list[0].foo()

    '--> This is an instance of class A with id 0x009011B0'

    If we don't tack on the parentheses we get the bound method (how
    that happens -- i.e. the automatic binding together of a class instance
    and a method of the class to make a bound method -- is central to how
    python's classes work, and something you will want to understand).

    >>> my_list[0].foo

    <bound method A.foo of <__main__.A object at 0x009011B0>>

    We can iterate through the list, which successively binds each object
    to the supplied name (obj here) via the reference from the list:
    >>> for obj in my_list: print obj.foo()

    ...
    --> This is an instance of class A with id 0x009011B0
    --> This is an instance of class A with id 0x009011F0
    >>>


    And show both together:
    >>> for obj in my_list: print obj, obj.foo()

    ...
    <__main__.A object at 0x009011B0> --> This is an instance of class A with id 0x009011B0
    <__main__.A object at 0x009011F0> --> This is an instance of class A with id 0x009011F0

    BTW, that first part is just the standard representation of object instances in text,
    and you can customize it via the __repr__ method, e.g.,

    >>> class A(object):

    ... def foo(self): return '--> This is an instance of class A with id 0x%08X'%id(self)
    ... def __repr__(self): return '<<My "A" instance at %x>>'%id(self)
    ...
    >>> my_list = [A() for dummy in range(2)]
    >>> my_list

    [<<My "A" instance at 9015f0>>, <<My "A" instance at 9011f0>>]
    >>> my_list[1]

    <<My "A" instance at 9011f0>>
    >>> my_list[1].foo()

    '--> This is an instance of class A with id 0x009011F0'
    >>> for obj in my_list: print obj, obj.foo()

    ...
    <<My "A" instance at 9015f0>> --> This is an instance of class A with id 0x009015F0
    <<My "A" instance at 9011f0>> --> This is an instance of class A with id 0x009011F0

    We can still get the old style representation by explicitly calling the base __repr__
    function we overrode:

    >>> for obj in my_list: print obj, object.__repr__(obj)

    ...
    <<My "A" instance at 9015f0>> <__main__.A object at 0x009015F0>
    <<My "A" instance at 9011f0>> <__main__.A object at 0x009011F0>
    >>>


    But note that the builtin repr function uses our custom repr as one would expect:

    >>> my_list[1]

    <<My "A" instance at 9011f0>>
    >>> repr(my_list[1])

    '<<My "A" instance at 9011f0>>'

    (The quotes are because interactively we are being shown that the result is a string,
    whereas the plain expression uses repr to get a string to print interactively, which
    prints the string plain). E.g.,

    >>> print repr(my_list[1])

    <<My "A" instance at 9011f0>>

    I encourage you and others to explore interactively, and when you get something you
    don't understand, copy from the screen and paste it into your posted question
    (which you apparently did with the list snippet -- bravo ;-)

    HTH

    Regards,
    Bengt Richter
     
    Bengt Richter, Nov 13, 2004
    #4
  5. On Friday 12 November 2004 07:59 am, wrote:
    > Hi
    >
    > I have a list containing several instance address, for example:
    >
    > [<JavaClass instance at 00BAC290>, <JavaClass instance at 00BB0D10>,
    > <JavaClass instance at 00BA5230>]
    >
    > I'd like to invoke a method on each of these instance but I don't know :
    >
    > 1. if its possible
    > 2. how to proceed


    Well, you don't do it with a pointer. ;-)

    Use the list's name, index, and call:

    if you have:
    >>> print mylist

    [<JavaClass instance at 00BAC290>, <JavaClass instance at 00BB0D10>,
    <JavaClass instance at 00BA5230>]

    you can do:

    mylist[0].mymethod(...)

    (where "..." is whatever arguments your methods take).

    You can also do things like

    mylist[0].__name__
    mylist[0].__module__
    dir(mylist[0])

    to look at the instance's metadata.

    I was thrown by this a bit when I first learned python too, but
    python doesn't care if you stack up operators like this, and the "."
    for invoking a method and the "()" for calling a callable object
    are just operators, along with indexing "[]" and so on. Mix and
    match.



    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
     
    Terry Hancock, Nov 13, 2004
    #5
    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. Replies:
    6
    Views:
    278
    Bruno Desthuilliers
    Jul 10, 2007
  2. candide
    Replies:
    65
    Views:
    1,403
  3. thunk
    Replies:
    1
    Views:
    315
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    487
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    625
    thunk
    Apr 3, 2010
Loading...

Share This Page