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

H

HYRY

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?

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.
 
S

Steve Holden

HYRY said:
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".
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:

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
 
H

HYRY

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.11863968
 
G

Gabriel Genellina

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()

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.
 
H

HYRY

"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:
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.
 
G

Gabriel Genellina

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.
 
B

Bruno Desthuilliers

HYRY a écrit :
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).
 
H

HYRY

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
 
F

Fredrik Lundh

HYRY said:
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>
 
H

Hrvoje Niksic

HYRY said:
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
.... return getattr(type(meth.__self__), meth.__name__)
....
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.
 
B

Bruno Desthuilliers

HYRY a écrit :
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)
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top