Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to get the actual address of a object

Reply
Thread Tools

How to get the actual address of a object

 
 
mujunshan@gmail.com
Guest
Posts: n/a
 
      10-24-2008
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
 
Reply With Quote
 
 
 
 
mujunshan@gmail.com
Guest
Posts: n/a
 
      10-24-2008
On 10月24日, 下午12时51分, mujuns...@gmail.com wrote:
> 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
 
Reply With Quote
 
 
 
 
James Mills
Guest
Posts: n/a
 
      10-24-2008
On Fri, Oct 24, 2008 at 2:51 PM, <> wrote:
> Hi,I have a strange idea:is there any way to get memory address of a
> object.


id(obj)

Example:

>>> x = 10
>>> id(x)

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:

>>> x = 10
>>> y = x
>>> id(x)

134536908
>>> id(y)

134536908
>>>


cheers
James

--
--
-- "Problems are solved by method"
 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      10-24-2008
On Fri, Oct 24, 2008 at 2:58 PM, <> wrote:
> 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:

>>> x = 2
>>> y = "2"
>>> z = int(y)
>>> x

2
>>> y

'2'
>>> z

2
>>>


cheers
James

--
--
-- "Problems are solved by method"
 
Reply With Quote
 
mujunshan@gmail.com
Guest
Posts: n/a
 
      10-24-2008
On 10月24日, 下午1时10分, "James Mills" <prolo...@shortcircuit.net.au>
wrote:
> On Fri, Oct 24, 2008 at 2:58 PM, <mujuns...@gmail.com> wrote:
> > 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:
>
>
>
> >>> x = 2
> >>> y = "2"
> >>> z = int(y)
> >>> x

> 2
> >>> y

> '2'
> >>> z

> 2
>
> cheers
> James
>
> --
> --
> -- "Problems are solved by method"


Thank you,James.
My original idea was to study all the contents of any object. I can do
it by using module ctypes.
 
Reply With Quote
 
James Mills
Guest
Posts: n/a
 
      10-24-2008
On Sat, Oct 25, 2008 at 12:25 AM, <> wrote:
> 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:

>>> x = 10
>>> dir(x)

['__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

--
--
-- "Problems are solved by method"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Finding out actual TCP/IP address UJ ASP .Net Web Services 6 08-30-2006 04:58 PM
Actual memory used by an object --- How to Determine? zooko@zooko.com Python 0 10-21-2004 12:39 PM
How do I hide my actual e-mail address and name? Joanne M. Milne Computer Support 6 09-12-2004 08:34 PM
Actual memory used by an object --- How to Determine? Avi Kak Python 1 02-20-2004 06:10 AM



Advertisments