Howto MACRO in python ?

Bengt Richter bokr at oz.net
Tue Aug 12 16:15:18 EDT 2003


On Tue, 12 Aug 2003 20:39:39 +0200, Christian Tismer <tismer at tismer.com> wrote:

>Richie Hindle wrote:
>
>...
>
>> Anyone know how I could retrieve function attributes from within a trace
>> function?  It would be neater.
>
>Do you really mean function attributes, or just
>local variables? The latter are easily obtainable
>through the frame's f_locals.
>
>If you are just after the locals, you don't need to read on.
>
>When a frame is executed, the function is no longer
>available, just the source filename (from the code
>object), the name of the function and the frame's f_lineno.
>
>Guessing the function object from this info is not
>guaranteed to work, but will probably work if your
>code is static (not created dynamically, compiled
>and thrown away after starting it).
>With some luck, you can use the filename and the
>function name, import the according module and
>get the function object by the name.
>Then you can use function attributes.
>
maybe there ought to be a magic __self__ in the local namespace of a function?

Or better yet (?;-) if a user-defined function were a subclass of type function
instead of an instance, and a frame were an activation-instance of this function subclass,
then frame.__class__.__dict__ would be the function __dict__, where you could find attributes.

Binding of locals to passed args would be an __init__ function of the activation-instance,
automatically followed by running the code (or maybe not, in the case of generators),
and presumably a function (being a frame-instance-making class) could be customized more
flexibly than we can now do with functions, including metaclass stuff, so you could e.g.
define pre-bound local slot variables (just handwaving ;-).  def would be syntactic sugar.

;-)

Regards,
Bengt Richter




More information about the Python-list mailing list