Question about math.pi is mutable

BartC bc at freeuk.com
Sun Nov 8 07:39:50 EST 2015


On 08/11/2015 11:50, Marko Rauhamaa wrote:
> BartC <bc at freeuk.com>:
>
>> On 08/11/2015 11:02, Marko Rauhamaa wrote:
>>> That elegant dynamism comes at a cost: method lookup is not a constant
>>> memory offset. Rather, it is a dictionary lookup.
>>
>> I've never understood why this seems to be necessary in Python. Why do
>> names have to be looked up? (I'm assuming this is searching by name in
>> some sort of table.)
>>
>> When a module is compiled, while the compiler can't see the
>> definitions inside the imported modules, it /will/ know all the names
>> that appear in this module, so it can organise them into fixed tables.
>> Then the names can be referred to by index. (I think LOAD_FAST does
>> this.)
>
> Modules are not the issue. Barely any functions are invoked from the
> modules. Rather, almost all invocations are object methods. (Of course,
> modules are objects, too.) Methods belong to objects that can be
> literally anything.
>
> Consider, for example,
>
>      shutil.copyfileobj(src, dst[, length])
>
> The shutil module has absolutely no idea what kind of objects src and
> dst are. An example program:
>
> ========================================================================
> 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.

-- 
Bartc



More information about the Python-list mailing list