468,268 Members | 1,540 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

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

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.

Sep 26 '07 #1
10 1819
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

Sep 26 '07 #2
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

Sep 26 '07 #3
En Wed, 26 Sep 2007 01:22:37 -0300, HYRY <ru*******@gmail.comescribi�:
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

Sep 26 '07 #4
"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.

Sep 26 '07 #5
En Wed, 26 Sep 2007 03:29:09 -0300, HYRY <ru*******@gmail.comescribi�:
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

Sep 26 '07 #6
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).

Sep 26 '07 #7
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

Sep 26 '07 #8
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>

Sep 26 '07 #9
HYRY <ru*******@gmail.comwrites:
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.
Sep 26 '07 #10
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)
Sep 26 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

117 posts views Thread by Steevo | last post: by
5 posts views Thread by Charles Krug | last post: by
77 posts views Thread by Ville Vainio | last post: by
4 posts views Thread by digitalorganics | last post: by
16 posts views Thread by devicerandom | last post: by
reply views Thread by DarrenWeber | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.