the address of list.append and list.append.__doc__

Discussion in 'Python' started by HYRY, Sep 26, 2007.

  1. HYRY

    HYRY Guest

    I have the following questions, I am using Python 2.4.2

    >>> a = [1,2,3]
    >>> id(a.append)

    19167152 #1
    >>> id(list.append)

    11306608 #1

    1. the address of a.append and list.append is different, can I get the
    address of list.append from a.append?


    >>> id(a.append.__doc__)

    19162720
    >>> id(a.append.__doc__)

    19162336
    >>> id(a.append.__doc__)

    19162592
    >>> id(a.append.__doc__)

    19162720
    >>> id(list.append.__doc__)

    19162336
    >>> id(list.append.__doc__)

    19162720
    >>> id(list.append.__doc__)

    19162592
    >>> id(list.append.__doc__)

    19162336
    2. why the address of a.append.__doc__ and list.append.__doc__ change,
    this means __doc__ is not a normal string, but something return a
    string.
    HYRY, Sep 26, 2007
    #1
    1. Advertising

  2. HYRY

    Steve Holden Guest

    HYRY wrote:
    > I have the following questions, I am using Python 2.4.2
    >
    >>>> a = [1,2,3]
    >>>> id(a.append)

    > 19167152 #1
    >>>> id(list.append)

    > 11306608 #1
    >
    > 1. the address of a.append and list.append is different, can I get the
    > address of list.append from a.append?
    >

    No. a.append is a "bound method" - a method that already has an
    associated instance, that will be provided as the first argument to the
    method call. Bound methods are created "on the fly".

    >
    >>>> id(a.append.__doc__)

    > 19162720
    >>>> id(a.append.__doc__)

    > 19162336
    >>>> id(a.append.__doc__)

    > 19162592
    >>>> id(a.append.__doc__)

    > 19162720
    >>>> id(list.append.__doc__)

    > 19162336
    >>>> id(list.append.__doc__)

    > 19162720
    >>>> id(list.append.__doc__)

    > 19162592
    >>>> id(list.append.__doc__)

    > 19162336
    > 2. why the address of a.append.__doc__ and list.append.__doc__ change,
    > this means __doc__ is not a normal string, but something return a
    > string.
    > Don't know. WJFFM on 2.5.1:


    >>> id(list.append.__doc__)

    2146574752
    >>> id(list.append.__doc__)

    2146574752
    >>> id(list.append.__doc__)

    2146574752
    >>> id(list.append.__doc__)

    2146574752
    >>>


    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden

    Sorry, the dog ate my .sigline
    Steve Holden, Sep 26, 2007
    #2
    1. Advertising

  3. HYRY

    HYRY Guest

    > No. a.append is a "bound method" - a method that already has an
    > associated instance, that will be provided as the first argument to the
    > method call. Bound methods are created "on the fly".
    >


    Does this means there is no method to get the original methods from
    the Bound methods created "on the fly"?

    I installed python 2.4.4 and tried id(list.append.__doc__) again, here
    is the result, only id(list.append.__doc__) changes, this is strange,
    and only happened in the command line. Because I am doing something
    program that use this id in the command line. Can someone help me try
    this on your PC, I want to know which is the problem: this version of
    python, or my PC.

    Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]
    on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> id(list.__doc__)

    11674984
    >>> id(list.__doc__)

    11674984
    >>> id(list.__doc__)

    11674984
    >>> id(list.append.__doc__)

    11747168
    >>> id(list.append.__doc__)

    11824352
    >>> id(list.append.__doc__)

    11747168
    >>> id(list.append.__doc__)

    11824352
    >>> id(list.append.__doc__)

    11747168
    >>> id(list.append.__doc__)

    11824352
    >>> id(list.count.__doc__)

    11863968
    >>> id(list.count.__doc__)

    11863968
    >>> id(list.count.__doc__)

    11863968
    >>> id(list.count.__doc__)

    11863968
    HYRY, Sep 26, 2007
    #3
  4. En Wed, 26 Sep 2007 01:22:37 -0300, HYRY <> escribi�:

    > I installed python 2.4.4 and tried id(list.append.__doc__) again, here
    > is the result, only id(list.append.__doc__) changes, this is strange,
    > and only happened in the command line. Because I am doing something
    > program that use this id in the command line. Can someone help me try
    > this on your PC, I want to know which is the problem: this version of
    > python, or my PC.


    "the problem"?
    Perhaps if you explain what you really want to do, someone can think the
    way to do that, most likely *not* using id()

    >>>> id(list.count.__doc__)

    > 11863968
    >>>> id(list.count.__doc__)

    > 11863968
    >>>> id(list.count.__doc__)

    > 11863968
    >>>> id(list.count.__doc__)

    > 11863968


    This behavior is an accident. The actual doc strings are stored as C
    null-terminated strings inside the C structures defining the list type.
    When you request list.append.__doc__, a Python string object has to be
    built pointing to the original C string. After the call to id(), nobody
    references that string object, and it is garbage collected. Next time you
    request the same thing, depending on the details on how the memory
    allocator works, it may or may not reuse the same memory address. Try with
    some print "hello" in between those id calls.

    --
    Gabriel Genellina
    Gabriel Genellina, Sep 26, 2007
    #4
  5. HYRY

    HYRY Guest

    > "the problem"?
    > Perhaps if you explain what you really want to do, someone can think the
    > way to do that, most likely *not* using id()


    Thanks, now I know I cannot use id() for my problem.

    Here is my problem:

    I want to add a docstring translator into the Python interpreter. If
    the user input:
    >>> a = [1,2,3]
    >>> a.append(

    this translator will show the docstring of append in my native
    language. Because __doc__ is read only, I added a dict to the
    interpreter as follows:

    DOC[list.append.__doc__] = """ translated version of the __doc__ """

    When it is the time to show docstring, the program get the translated
    version from DOC.
    This works, but I think the key of DOC is too long, so I want to use
    the id of list.append.__doc__ as the key; or use the id of
    list.append:

    DOC[id(list.append.__doc__)] = "..."
    DOC[id(list.append)] = "..."

    So, I asked how to get list.append from a.append, and why
    id(list.append.__doc__) changes.
    HYRY, Sep 26, 2007
    #5
  6. En Wed, 26 Sep 2007 03:29:09 -0300, HYRY <> escribi�:

    > I want to add a docstring translator into the Python interpreter. If
    > the user input:
    >>>> a = [1,2,3]
    >>>> a.append(

    > this translator will show the docstring of append in my native
    > language. Because __doc__ is read only, I added a dict to the
    > interpreter as follows:
    >
    > DOC[list.append.__doc__] = """ translated version of the __doc__ """
    >
    > When it is the time to show docstring, the program get the translated
    > version from DOC.
    > This works, but I think the key of DOC is too long, so I want to use
    > the id of list.append.__doc__ as the key; or use the id of
    > list.append:


    Don't worry about that. There is no "wasted memory" apart from some
    overhead due to the string object itself (a few bytes per string, fixed
    and not depending on the string length).
    Just use the __doc__ as the dictionary key. Perhaps using an external
    database, to avoid keeping all the translated texts in memory.

    --
    Gabriel Genellina
    Gabriel Genellina, Sep 26, 2007
    #6
  7. HYRY a écrit :
    >> No. a.append is a "bound method" - a method that already has an
    >> associated instance, that will be provided as the first argument to the
    >> method call. Bound methods are created "on the fly".
    >>

    >
    > Does this means there is no method to get the original methods from
    > the Bound methods created "on the fly"?


    There's no such thing as an "original method" - what's stored as an
    attribute of the class is a plain function. FWIW, you can get at this
    function quite easily - via the im_func attribute of the method.

    Now what I wonder is what you want to do with the internal identifier of
    a function or method ? (please not that the use of the memory address as
    an id is purely an implementation detail of CPython).
    Bruno Desthuilliers, Sep 26, 2007
    #7
  8. HYRY

    HYRY Guest

    > There's no such thing as an "original method" - what's stored as an
    > attribute of the class is a plain function. FWIW, you can get at this
    > function quite easily - via the im_func attribute of the method.


    I know about im_func, but I tried the im_func attribute of append and
    I get error: 'builtin_function_or_method' object has no attribute
    'im_func'
    a = [1,2,3]
    a.append.im_func # error

    > Now what I wonder is what you want to do with the internal identifier of
    > a function or method ? (please not that the use of the memory address as
    > an id is purely an implementation detail of CPython).


    I want to use the function( or id of the function) as a key of dict,
    d = {}
    d[list.append] = "abc"
    d[str.find] = "..."

    and I want a function f, that return list.append when call as
    f(a.append), so I can get the value in d by d[f(a.append)].

    And I also find these is interesting, methods of an unmutable object
    can be used as key, but methods of a mutable object cannot:

    a = [1,2,3]
    d[a.append] = "..." # error: list objects are unhashable
    b = "123"
    d[b.find] = "..." # OK
    HYRY, Sep 26, 2007
    #8
  9. HYRY wrote:

    > This works, but I think the key of DOC is too long, so I want to use
    > the id of list.append.__doc__ as the key; or use the id of
    > list.append:
    >
    > DOC[id(list.append.__doc__)] = "..."
    > DOC[id(list.append)] = "..."
    >
    > So, I asked how to get list.append from a.append, and why
    > id(list.append.__doc__) changes.


    dictionaries hold *references* to objects, not copies of the object
    values, so that won't save you anything.

    </F>
    Fredrik Lundh, Sep 26, 2007
    #9
  10. HYRY <> writes:

    > This works, but I think the key of DOC is too long, so I want to use
    > the id of list.append.__doc__ as the key; or use the id of
    > list.append:


    Using the id is not a good idea because id's are not permanent. Using
    list.append as the hash key will work and will internally use the
    pointer to produce the hash key, which is probably what you want
    anyway.

    > So, I asked how to get list.append from a.append


    >>> def unbound(meth):

    .... return getattr(type(meth.__self__), meth.__name__)
    ....
    >>> unbound(a.append)

    <method 'append' of 'list' objects>

    > and why id(list.append.__doc__) changes.


    Because the doc for builtins is internally kept in a read-only C
    string for efficiency. The Python string is built only when actually
    used.
    Hrvoje Niksic, Sep 26, 2007
    #10
  11. HYRY a écrit :
    >> There's no such thing as an "original method" - what's stored as an
    >> attribute of the class is a plain function. FWIW, you can get at this
    >> function quite easily - via the im_func attribute of the method.

    >
    > I know about im_func, but I tried the im_func attribute of append and
    > I get error: 'builtin_function_or_method' object has no attribute
    > 'im_func'


    Hmm, yes, of course. builtin C implementation here...

    (snip use case)
    Bruno Desthuilliers, Sep 26, 2007
    #11
    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. Trevor Taylor

    __doc__ of current function?

    Trevor Taylor, Sep 2, 2004, in forum: Python
    Replies:
    3
    Views:
    581
    Sean Ross
    Sep 2, 2004
  2. David Isaac

    property __doc__

    David Isaac, Jul 1, 2006, in forum: Python
    Replies:
    1
    Views:
    335
    Robert Kern
    Jul 1, 2006
  3. Gabriel Genellina

    __doc__ in compiled script

    Gabriel Genellina, Nov 2, 2006, in forum: Python
    Replies:
    5
    Views:
    269
    Peter Otten
    Nov 3, 2006
  4. gtb

    Printing __doc__

    gtb, Mar 21, 2007, in forum: Python
    Replies:
    7
    Views:
    289
    Bart Ogryczak
    Mar 22, 2007
  5. James T. Dennis
    Replies:
    9
    Views:
    316
    Dennis Lee Bieber
    May 11, 2007
Loading...

Share This Page