Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > the address of list.append and list.append.__doc__

Reply
Thread Tools

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

 
 
HYRY
Guest
Posts: n/a
 
      09-26-2007
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.

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      09-26-2007
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

 
Reply With Quote
 
 
 
 
HYRY
Guest
Posts: n/a
 
      09-26-2007
> 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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      09-26-2007
En Wed, 26 Sep 2007 01:22:37 -0300, HYRY <(E-Mail Removed)> 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

 
Reply With Quote
 
HYRY
Guest
Posts: n/a
 
      09-26-2007
> "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.

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      09-26-2007
En Wed, 26 Sep 2007 03:29:09 -0300, HYRY <(E-Mail Removed)> 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

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-26-2007
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).

 
Reply With Quote
 
HYRY
Guest
Posts: n/a
 
      09-26-2007
> 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

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-26-2007
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>

 
Reply With Quote
 
Hrvoje Niksic
Guest
Posts: n/a
 
      09-26-2007
HYRY <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify IP address from a text file and replace it with new address. Chandu80 Ruby 4 11-09-2010 07:16 AM
PIX Firewall MAC address VPN IP address Julian Dragut Cisco 1 02-07-2006 07:57 AM
Pass a Web Address and return a File System Address =?Utf-8?B?QW5nZWw=?= ASP .Net 2 09-20-2005 11:35 PM
obtaining the IP ADDRESS of an IP POHNE by its MAC ADDRESS ProgDario Cisco 17 05-06-2005 02:32 PM
Routing to public IP of NAT address from internal NAT address Andrew Albert Cisco 1 02-08-2005 07:05 PM



Advertisments