[Python-Dev] towards a faster Python

Neil Schemenauer nas@python.ca
Mon, 9 Jun 2003 12:18:21 -0700


Hi guys,

With Guido's blessing, I just commited a change that takes a small step
towards where we want to be, IMO.  Using __setattr__ on a module to
create a global name that shadows a builtin name now raises a
DeprecationWarning.  E.g.

    import fileinput
    fileinput.open = 10 # <- warning will be raised

The goal is that the compiler should be able to determine the scope of a
name at compile time.  Code like the above does not allow that and so
non-local names must be searched for in both globals and builtins at run
time.  Unfortunately the warning is not bulletproof.  It's possible to
modify the module dict directly and bypass the warning.  I'm not sure
what to do about that. :-(

Eventually we might want to allow optimizations based on known builtin
functions.  For example, code like

    for i in range(100000):
        ...

could be rewritten to use an integer counter rather than creating a list
and then iterating over it.  I'm not sure how much resistance there
would be in the community to disallowing reassignment of builtin names
though.  I don't think I've ever reassigned a builtin name so I think it
would be worth it.

The compiler could also warn about references to non-existent names.
That's a common error and would be helpful to developers.

Looking farther into the future, knowing that a name refers to a
specific builtin function would give a type inference system a lot more
information to work with.  I guess it would help Psyco.

Just some ideas to think about.

   Neil