Question about math.pi is mutable

Marko Rauhamaa marko at pacujo.net
Sun Nov 8 07:43:37 EST 2015


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.


Marko



More information about the Python-list mailing list