How to get the actual address of a object

M

mujunshan

Hi,I have a strange idea:is there any way to get memory address of a
object.

For example:

i = 10

addr = get_address(i)
address will be assigned a integer which is pointer of object
i,then I want to recast addr into another integer:

j = cast(addr,<type int>)

I think it is easy to get it by modifying source code of python,but I
would like to know if there was an existing one.

many thanks
 
M

mujunshan

Hi,I have a strange idea:is there any way to get memory address of a
object.

For example:

i = 10

addr = get_address(i)
address will be assigned a integer which is pointer of object
i,then I want to recast addr into another integer:

j = cast(addr,<type int>)

I think it is easy to get it by modifying source code of python,but I
would like to know if there was an existing one.

many thanks

maybe id(x) can get it ,but how to cast it back into a object
 
J

James Mills

Hi,I have a strange idea:is there any way to get memory address of a
object.
id(obj)

Example:
134536908

But this probably (most likely) isn't it's address in memory
but more it's unique identifier that separates it from every
other object in memory.
j = cast(addr,<type int>)

You can't do this in Python afaik nor would you want to.

If you simply want to copy an object, do this:

cheers
James
 
J

James Mills

maybe id(x) can get it ,but how to cast it back into a object

You can't. Python is NOT C/C++/Java or whatever.

If you have a variable, x, and you want to "copy" it
to another variable, y. Use assignment.

Most (if not all) objects in python a referenced.
A lot of types are also immutable.

Describe your problem, perhaps we may be able to
provide you a "better" solution ? Can I statically re-cast
an object into a different type by getting the address
of another object .... is not a very good problem.

If you're after, coercing one type into another, for example:

cheers
James
 
M

mujunshan

You can't. Python is NOT C/C++/Java or whatever.

If you have a variable, x, and you want to "copy" it
to another variable, y. Use assignment.

Most (if not all) objects in python a referenced.
A lot of types are also immutable.

Describe your problem, perhaps we may be able to
provide you a "better" solution ? Can I statically re-cast
an object into a different type by getting the address
of another object .... is not a very good problem.

If you're after, coercing one type into another, for example:



2

cheers
James

Thank you,James.
My original idea was to study all the contents of any object. I can do
it by using module ctypes.
 
J

James Mills

Thank you,James.
My original idea was to study all the contents of any object. I can do
it by using module ctypes.

You can simply just query it's attributes.

Use __dict__ or dir(obj)

Example:
['__abs__', '__add__', '__and__', '__class__', '__cmp__',
'__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__',
'__float__', '__floordiv__', '__getattribute__', '__getnewargs__',
'__hash__', '__hex__', '__index__', '__init__', '__int__',
'__invert__', '__long__', '__lshift__', '__mod__', '__mul__',
'__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__',
'__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__',
'__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__',
'__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__',
'__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__',
'__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']
cheers
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top