A better self

Alex Martelli aleax at aleax.it
Mon Jul 22 01:58:02 EDT 2002


Delaney, Timothy wrote:

>> From: Alex Martelli [mailto:aleax at aleax.it]
>> 
>> The point is that SelferMeta.__new__ gets to look at all of
>> class Myself's dictionary *and tamper with it as needed* before
>> delegating the rest to class type.  Specifically, it would get
        ...
> Wouldn't work in Jython ... (when Jython gets to 2.2 ...).

Or more precisely: the bytecode manipulations needed will be specific
to the underlying virtual machine.  I.e., unless a VM is designed in
really crazy ways, it will probably be possible to use exactly the
same technique (metaclass computing what bytecode is needed to achieve
the desired results and substituting it in place of the bytecode as
given), but the details of the transformations will change.

Source-to-source transformations would be more general, but getting
at the relevant sources is not trivial.  When Python is run with -O
you lose the line-numbers; when it's run with -OO you even lose the
docstrings, so hiding semantically crucial info there is not fully 
kosher either.  Unfortunately, the metaclass gets ahold of things
only AFTER the class body is done.  I suspect the only techniques
that would let you use the same metaclass independently of underlying
VM and runtime options are those that boil down to putting the
source in string attributes of the class object, e.g.:

class Selfish(SourceSelfer):
    __slots__ = "x y z".split()

    amethod = """(t, u, v):
        return x*t + u*y + z*v
    """


Alex




More information about the Python-list mailing list