How to get the actual address of a object

Discussion in 'Python' started by mujunshan@gmail.com, Oct 24, 2008.

  1. Guest

    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
     
    , Oct 24, 2008
    #1
    1. Advertising

  2. Guest

    On 10ÔÂ24ÈÕ, ÏÂÎç12ʱ51·Ö, 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
     
    , Oct 24, 2008
    #2
    1. Advertising

  3. James Mills Guest

    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"
     
    James Mills, Oct 24, 2008
    #3
  4. James Mills Guest

    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"
     
    James Mills, Oct 24, 2008
    #4
  5. Guest

    On 10ÔÂ24ÈÕ, ÏÂÎç1ʱ10·Ö, "James Mills" <>
    wrote:
    > 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"


    Thank you,James.
    My original idea was to study all the contents of any object. I can do
    it by using module ctypes.
     
    , Oct 24, 2008
    #5
  6. James Mills Guest

    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"
     
    James Mills, Oct 24, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Avi Kak
    Replies:
    1
    Views:
    247
    Josiah Carlson
    Feb 20, 2004
  2. Replies:
    0
    Views:
    265
  3. Josiah Carlson
    Replies:
    2
    Views:
    290
    Josiah Carlson
    Oct 21, 2004
  4. beginner16
    Replies:
    2
    Views:
    665
    A. Bolmarcich
    Aug 1, 2007
  5. UJ

    Finding out actual TCP/IP address

    UJ, Aug 27, 2006, in forum: ASP .Net Web Services
    Replies:
    6
    Views:
    171
    John Saunders
    Aug 30, 2006
Loading...

Share This Page