2

I want to get the instance of an class object. I have its address and the name of the object. How can it be possible to create the instance of the object or how can I retrieve the same object?

The class is defined in one python module and i want it to be used in another python module.

Can I use object id to access the object of the class.?


My actual problem is that I have a Python module in which a class had been defined (class Myclass). I am reusing the module in my new code. I want the object of Myclass, which is defined in the old module, to be accessed in my new file.

Can anyone suggest me a way to achieve this? I don't have right to access the module source code where the class has been defined. I can manage to get the class name with the garbage collector.

mkreddy
  • 163
  • 3
  • 5
  • 12
  • 1
    The object id does not necessarily have anything to do with the internal address where the object lives in memory. So no, it’s not possible to get a Python object from the id, or from any “address” (whereever you get that from). Instead, you should just make a dictionary somewhere, where you store your objects if you want to look them up later. – poke Sep 26 '13 at 16:17
  • If you have the address of an object (by which I assume you mean a reference to it?) then that's all you need to talk to the object. – Joe Sep 26 '13 at 16:18
  • Hello Joe, I don't have the reference of the object. I just had the physical memory address (0X14356700). And one more thing My original class is defined in the file where i had no access to the source. I just had the binary of it to access or to reuse the code. – mkreddy Sep 26 '13 at 16:21
  • 2
    @mkreddy Have you read: [Python: Get object by id](http://stackoverflow.com/questions/1396668/python-get-object-by-id)? – Ashwini Chaudhary Sep 26 '13 at 16:22
  • The `id()` of an object and the address of it in memory are not the same thing. They happen to be in CPython, but that is an implementation detail. – roippi Sep 26 '13 at 16:22
  • Tip: it looks like you've fallen into the [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Instead of asking about your actual problem, you came up with a possible solution and then asked about how to get that to work. We've all made this mistake from time to time, which is why it's always a good idea to give the "actual problem" too. Often someone else can point out a much better approach which you'd never have heard of if you only asked a very narrow question. – DSM Sep 26 '13 at 16:51

3 Answers3

2

By the id you can't. By name:

class A:
    def __init__(self,val):
        self.v = val
myInst = A(3)
print eval("myInst").v
>>> 3
Veedrac
  • 58,273
  • 15
  • 112
  • 169
Nacib Neme
  • 859
  • 1
  • 17
  • 28
  • That only works if the variable holding a reference to that object is actually in scope. – poke Sep 26 '13 at 16:37
2

id is only defined as a number *unique to the element among currently existing elements. Some Python implementations (in fact, all main ones but CPython) do not return the memory address.

%~> pypy
Python 2.7.3 (480845e6b1dd219d0944e30f62b01da378437c6c, Aug 08 2013, 17:02:19)
[PyPy 2.1.0 with GCC 4.8.1 20130725 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``arguably, everything is a niche''
>>>> a = 1
>>>> b = 2
>>>> c = 3
>>>> id(a)
9L
>>>> id(b)
17L
>>>> id(c)
25L

So you have to guarantee that it is the memory address. Furthermore, because of this Python provides no id → object mapping, especially as the object that an id maps to can be changed if the original is deleted.

You have to ask why you're holding the id. If it's for space reasons, bear in mind that containers actually hold references to items, so [a, a, a, a, a] actually takes less space than [id(a), id(a), id(a), id(a), id(a)]; a.

You can consider also making a dict of {id: val} for all the relevant items and storing that. This will keep val alive, so you can use weakrefs to allow the vals to be garbage collected. Remember, use weakref if you want a weakref.

Veedrac
  • 58,273
  • 15
  • 112
  • 169
  • Thank you all for the replies. My actual problem is " I have a python module in which a class had been defined "Class Myclass". I am reusing the module in my new code. I want the object of Myclass which is defined in the old module have to be accessed in my new file. Can any one suggest me a way to achieve this. I don't have right to access the module where the class has defined. I can mange to get the class name with garbage collector. – mkreddy Sep 26 '13 at 16:36
2

My actual problem is " I have a python module in which a class had been defined "Class Myclass". I am reusing the module in my new code. I want the object of Myclass which is defined in the old module have to be accessed in my new file. Can any one suggest me a way to achieve this. I don't have right to access the module where the class has defined. I can mange to get the class name with garbage collector.

If you are reusing the module, then you have access to that module.

For example if said module creates the object like this:

class MyClass:
    pass

myobj = MyClass()

Then you can import that reference too when you import the module:

from othermodule import MyClass, myobj
poke
  • 369,085
  • 72
  • 557
  • 602