473,473 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2088
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
by: Steevo | last post by:
Any suggestions as to the best programs for cloaking email addresses? Many thanks -- Steevo
5
by: Charles Krug | last post by:
List: I have this: # classC.py class C(object): pass class D(C): pass
77
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it,...
4
by: digitalorganics | last post by:
Is there a method or attribute I can use to get a list of classes defined or in-use within my python program? I tried using pyclbr and readmodule but for reason that is dogslow. Thanks in...
2
by: Phoe6 | last post by:
Hi all, Part of my script is to check for pre-requisite rpms to be installed. If its installed, I just display the rpm version as in rpm database, otherwise I output a message saying: ' rpm is...
16
by: devicerandom | last post by:
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. ...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
6
by: SanPy | last post by:
The subject of this message might be a little cryptic, so here's an example of what I mean: def foo(): """doc string of foo""" print foo.__doc__ doc string of foo What I want to know is...
1
by: Lpitt56 | last post by:
I am running MS Access 2007 and I want to update an Outlook Address book from my Access Database. I started out by importing the Outlook Address Book as a linked table and it linked fine. I then...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.