Accessing an instance via its memory address (instance at ...)
Terry Hancock
hancock at anansispaceworks.com
Sat Nov 13 14:42:31 EST 2004
On Friday 12 November 2004 07:59 am, simon.alexandre at cetic.be 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
More information about the Python-list
mailing list