Deep Black Magic in Python: please help

Jan Burgy jburgy at hotmail.com
Mon Aug 16 03:04:41 EDT 2004


"Terry Reedy" <tjreedy at udel.edu> wrote in message news:<mailman.1629.1092425178.5135.python-list at python.org>...
> "Jan Burgy" <jburgy at hotmail.com> wrote in message
> news:807692de.0408130310.6e3ed9db at posting.google.com...
> >         return arg_dict["one"] + self.bar(arg_dict)
>  
> > It shouldn't be all that hard. My problem is the lookup of 'self.bar'. In
> > the AST it looks something like
> >
> >     CallFunc(Getattr(Name('self'), 'bar'), [Name('arg_dict')], None,
>  None)
> >
> > How do I find the corresponding function?
> 
> The lookup returns a bound method, whose structure is an implementation
> detail.
> 
> Completely untested suggestion, possibly not correct or possible:
> 
> Write an implementation-specific unwrap function.  Then either augment the
> source code
> 
> .... + unwrap(self.bar)(self, arg_dict) # self now an explict arg
> 
> or the analysis code to do the unwrapping.  For the latter, recognize
> Name('self') and replace the result of Getattr(Name('self'), 'bar') with
> unwrap(thereof) and, again, augment the arg list.
> 
> Terry J. Reedy

Hi Terry,

Thank you very much for your suggestion. I am not sure how it would
help though. What is preventing me now from already replacing the
result of Getattr(Name('self'), 'bar') with what I need? That's
precisely my problem however. Because Python is a dynamically typed
language, I have no information about 'self' at compile-time. Short of
saving context information while I am visiting the parse tree, I don't
know how to tell what class I am in.

Would-Meta-Classes-help-me-in-this-case-ly yours?

Jan



More information about the Python-list mailing list