Question about math.pi is mutable

Marko Rauhamaa marko at pacujo.net
Sun Nov 8 06:50:35 EST 2015


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())
========================================================================


Marko



More information about the Python-list mailing list