[Python-Dev] PEP: __source__ proposal

"Martin v. Löwis" martin at v.loewis.de
Sat Dec 4 00:26:33 CET 2004


Stelios Xanthakis wrote:
> Now that 2.4 is out and everything maybe it's
> about time to start discussing the "use the
> __source__ Luke" feature which IMO will really
> boost python into a new domain of exciting
> possibilities.

I'm opposed to this idea. It creates overhead in
the size of .pyc files, for no additional value
that couldn't be obtained otherwise.

As the rationale, the PEP lists:

1.
>     It is generally a tempting idea to use python as an interface to
>     a program.

I cannot see how this rationale is related to the PEP. You
can use Python as interface to a program with or without
__source__.

2.
>     The developers can implement all the functionality
>     and instead of designing a user interface, provide a python
>     interpreter to their users.

This does not require __source, either.

3.
>     A much more powerful approach would be an interface which is
>     dynamically constructed by the user to meet the user's needs.

Dynamic code generation doesn't require __source__, either.

4.
>     The most common development cycle of python programs is:
>     write .py file - execute .py file - exit - enhance .py file -
>     execute .py file - etc.  With the implementation of the __source__
>     attribute though the development/modification of python code
>     can happen at run-time.

This works just fine as well at the moment; see IDLE for an
example.

>     Functions and classes can be defined,
>     modified or enhanced while the python shell is running and
>     all the changes can be saved by saving the __source__ attribute
>     of globals before termination.

Currently, you can define classes dynamically, and you can also save
the source code of the class to a file in case you need it later.

 >     Moreover, in such a system
>     it is possible to modify the "code modification routines" and
>     eventually we have a self-modifying interface.  Using a
>     program also means improving its usability.

Self-modifying source code is currently also possible. Just read
the old source code from a .py file, modify it, and recompile it.

>     The current solution of using 'inspect' to get the source
>     code of functions is not adequate because it doesn't work
>     for code defined with "exec" and it doesn't have the source
>     of functions/classes defined in the interactive mode.

I fail to see why it isn't adequate. Anybody who wants to modify
source code that was originally passed to exec just needs to
preserve a copy of the source code, separately.

 >     Generally,
>     a "file" is something too abstract.  What is more real is the
>     data received by the python parser and that is what is stored
>     in __source__.

Not at all. A file is precisely the level of granularity that is
burnt into the Python language. A module is *always* a file, executed
from top to bottom. It is not possible to recreate the source code
of a module if you have only the source code of all functions, and
all classes.

Regards,
Martin


More information about the Python-Dev mailing list