Question about math.pi is mutable

BartC bc at freeuk.com
Sun Nov 8 08:42:51 EST 2015


On 08/11/2015 12:43, Marko Rauhamaa wrote:
> BartC <bc at freeuk.com>:
>
>> On 08/11/2015 11:50, Marko Rauhamaa wrote:
>>> ========================================================================
>>> import shutil
>>>
>>> class Source:
>>>       def __init__(self):
>>>           self.remaining = "hello world"
>>>
>>>       def read(self, count):
>>>           if count <= 0:
>>>               return ""
>>>           chunk, self.remaining = self.remaining[:count], self.remaining[count:]
>>>           return chunk
>>>
>>> class Dest:
>>>       def write(self, stuff):
>>>           print("<{}>".format(stuff))
>>>
>>> shutil.copyfileobj(Source(), Dest())
>>> ========================================================================
>>
>> OK, so here, it is necessary to resolve "copyfileobj" by seeing if
>> "shutil" is something that contains a name "copyfileobj" that happens
>> to be a method.
>>
>> Is this the lookup you're talking about, and is it done using the
>> actual string "copyfileobj"? If so, does it need to be done every
>> single time this line is executed? It would extraordinarily
>> inefficient if that was the case.
>
> No, what I'm talking about is that copyfileobj calls src.read() and
> dst.write() without having any idea what kinds of objects src and dst
> are. Thus, it will be difficult for a performance optimizer to get rid
> of the dictionary lookups.

Sorry, you'll have to assume I'm very stupid.

What exactly is being looked up, and in what?

 From what I can understand in your example:

* You are calling shutil.copyfileobj with two arguments, which happen to 
be instances of classes Source and Dest.

* Let's say these are known as src and dst inside .copyfileobj.

* .copyfileobj then calls methods src.read() dst.write().

* Each of these method calls has the form A.B() which seems to me to be 
little different from shutil.copyfileobj().

So to get back to what I was saying, does this lookup involving 
searching for method B in object A, and if so, does it actually do a 
search by name?

(Someone mentioned a precalculated hash, of "A", or of "src" or "dst", 
use used, but still, it's looking something up in a table, and a hash 
table lookup I believe still requires an string compare to check if 
you've got the right entry.)

-- 
Bartc



More information about the Python-list mailing list