473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Jul 18 '05 #1
4 3891
si*************@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 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
Jul 18 '05 #2

<si*************@cetic.be> wrote in message
news:cn**********@ikaria.belnet.be...
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

Jul 18 '05 #3
On Fri, 12 Nov 2004 14:59:54 +0100, <si*************@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>] ^^^^^^^^--[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
Jul 18 '05 #4
On Friday 12 November 2004 07:59 am, si*************@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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: [RaZoR] | last post by:
hello, main script creates IE window, put an array there and then calls a child script. chils script makes an array element equal to some object and returns control to main script. then main...
13
by: lupher cypher | last post by:
Hi, I'm trying to access memory directly at 0xb800 (text screen). I tried this: char far* screen = (char far*)0xb8000000; but apparently c++ compiler doesn't know "far" (says "syntax error...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
8
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
3
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is...
5
by: Mike | last post by:
I'm having trouble accessing SQL2005 Standard Edition as a second instance of SQL Server where the first instance is SQL 2000 Enterprise Edition. I installed SQL 2005 as a named instance...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
20
by: sethukr | last post by:
hi, i need to store a value to a particular memory location without having a variable. So, how can i access a 'memory address' without using variables?? Is it possible in C??? Plz, help...
3
by: john | last post by:
I would like to view physical memory that is mapped to a pci board. I am using a tool called WinIO to try to create a virtual address to that physical memory. It works for both read and write of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.