473,396 Members | 2,018 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 Objects Based On Their ID

This is probably obvious to you Python Geniuses (tm) out there but,
it is very late and I am experiencing Brain Fade:

Given the ID of an object, is there a way to access it? For example,
if we have the ID of a class instance, is there a way to invoke its
methods and attributes knowning only that ID? Similarly, if we have the
ID of a function, is there a way to call it?

This comes up because of an implementation I had in mind wherein I
would store the IDs of a notionally linked-list of objects - but without
the link - I just want to store their IDs in the desired order. But later,
when I want to actually use the objects I need a way to get from ID back
to something accessible in the namespace...

TIA,
--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

Jul 18 '05 #1
5 1148
Given the ID of an object, is there a way to access it? For example,
if we have the ID of a class instance, is there a way to invoke its
methods and attributes knowning only that ID? Similarly, if we have the
ID of a function, is there a way to call it?
No.
This comes up because of an implementation I had in mind wherein I
would store the IDs of a notionally linked-list of objects - but without
the link - I just want to store their IDs in the desired order. But
later, when I want to actually use the objects I need a way to get from ID
back to something accessible in the namespace...


Why only the id? A list only stores a reference to the object anyway - no
copy of it. So you don't gain anything by using the id.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Tim Daneliuk wrote:
This is probably obvious to you Python Geniuses (tm) out there but,
it is very late and I am experiencing Brain Fade:

Given the ID of an object, is there a way to access it?
short answer: no.

longer answer: write a small C extension that casts an integer (or long integer)
argument to a PyObject, increments the refcount, and returns the object. or
use gc.get_objects() to get a list of all GC-aware objects, and see if your object
is in there. etc. all solutions are fragile, non-portable, and/or inefficient.
This comes up because of an implementation I had in mind wherein I
would store the IDs of a notionally linked-list of objects - but without
the link - I just want to store their IDs in the desired order. But later,
when I want to actually use the objects I need a way to get from ID back
to something accessible in the namespace...


sounds like "import weakref" might be what you need.

</F>

Jul 18 '05 #3
> longer answer: write a small C extension that casts an integer (or long integer)
argument to a PyObject, increments the refcount, and returns the object.


here's a pointer to a pointer to such a function, btw:

http://mail.python.org/pipermail/pyt...er/013715.html

(see other messages in that thread for more on why this is a stupid idea)

</F>

Jul 18 '05 #4
Diez B. Roggisch wrote:
Given the ID of an object, is there a way to access it? For example,
if we have the ID of a class instance, is there a way to invoke its
methods and attributes knowning only that ID? Similarly, if we have the
ID of a function, is there a way to call it?

No.

This comes up because of an implementation I had in mind wherein I
would store the IDs of a notionally linked-list of objects - but without
the link - I just want to store their IDs in the desired order. But
later, when I want to actually use the objects I need a way to get from ID
back to something accessible in the namespace...

Why only the id? A list only stores a reference to the object anyway - no
copy of it. So you don't gain anything by using the id.


Point taken... thanks.

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jul 18 '05 #5

"Tim Daneliuk" <tu****@tundraware.com> wrote in message
news:uc*************@eskimo.tundraware.com...
Given the ID of an object, is there a way to access it?

You got the right short answer already.
Another long answer 'yes' is to keep a dictionary mapping ids to objects,
but as Diez pointed out, you need a real reason to do this.

In some respects, id() is like a siren (in the mythological sense) that
Python would be better off without.

Terry J. Reedy

Jul 18 '05 #6

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

Similar topics

4
by: keepyourstupidspam | last post by:
Hello, I have a class X with a member function setConfigValues(). I want to access this member function in another class Y. Y is not inherited from X. A member function of class X called run()...
4
by: Dave Veeneman | last post by:
I have an object hierarchy that's several layers deep, and I'm puzzling over how to access an object at any point in the hierarchy, solely by its key. The hierarchy is similar to an...
38
by: Radi Radichev | last post by:
Hi! I'm making a database application and i heard from a friend that it is more proffecional and easy to do this with bussines objects. Can anyone tell me where i can find more info on bussines...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
25
by: Penelope Dramas | last post by:
Hello, I'm in a front of very serious .net redesign/rewrite of an old VB6 application. I had been asked to make it .NET 2.0 and would like to ask couple of questions regarding data access as...
8
by: =?Utf-8?B?RGF2aWQrKw==?= | last post by:
Hi, I have been developing Web Services in VS2005. Usually I use the built in server in VS2005 to test and develop the Web Service. However I now want to make this Web Service available to...
1
by: Peter | last post by:
Hi, I have a Dictionary<key, valuewhich is accessed by three threads. One thread puts my value objects in the dictionary (occasionally), and also updates the contents of existing value objects -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.