[Baypiggies] puzzle: binding invocation context for a function call during a class declaration

Bill Janssen janssen at parc.com
Fri Dec 31 22:23:24 CET 2010


You're suggesting I do everything with attributes.  Yes, that's a possibility,
but it doesn't really answer the question I raised, which is about capturing the
invocation context for a function call that happens during the definition of a
class.

Bill

Zachary Collins <recursive.cookie.jar at gmail.com> wrote:

> You're trying to create more complicated symantics than is necessary
> to solve this problem.
> 
> How about
> class Foo (Concept):
>   things_i_am = [A, B]
> 
> ?
> 
> 2010/12/31 Bill Janssen <janssen at parc.com>:
> > I'm puzzling out how one would ape a declarative language in Python.
> > For instance, take a language that has declarations like
> >
> >  concept Foo {
> >    is-a A;
> >    is-a B;
> >    part-of X;
> >
> >    public int counter;
> >    public analogue bar;
> >  }
> >
> >  expression Bletch {
> >
> >    expresses Foo;
> >
> >    counter = 3;
> >  }
> >
> > where "concept", "public", "is-a", "part-of", "int", "analogue",
> > "expression", and "expresses" are all keywords.
> >
> > Since it's largely declarative, I'd naturally express it in Python as a
> > set of class declarations:
> >
> >  from mylang import is_a, part_of, Concept, Expression, attribute, public, analogue, expresses
> >
> >  class Foo (Concept):
> >
> >    is_a(A);
> >    is_a(B);
> >    part_of(X);
> >
> >    counter = attribute(public, int)
> >    bar = attribute(public, analogue)
> >
> >  class Bletch (Expression):
> >
> >    expresses(Foo)
> >    counter = 3
> >
> > Now, my question is, how can I capture the invocation context of a
> > function, so that I can know that "is_a(A)" is being called as part of
> > the definition of class "Foo"?  Do functions called during the
> > evaluation of a class leave their return result somewhere, for instance,
> > so that I could define a metaclass to do something with that result?
> >
> > Right now, I'm using the fragile workaround of
> >
> >  _relationships = []
> >  def _store_relationship (x, rel):
> >      primary = inspect.stack()[2][0].f_code.co_name
> >      _relationships.append((primary, rel, x))
> >  is_a = lambda x: _store_relationship(x, "is-a")
> >
> > If I was willing to use Python 3, I could use class decorators instead,
> > I suppose.  But I'd like to nest these declarations inside the class
> > definition if I can.
> >
> > Any ideas would be appreciated.
> >
> > Bill
> > _______________________________________________
> > Baypiggies mailing list
> > Baypiggies at python.org
> > To change your subscription options or unsubscribe:
> > http://mail.python.org/mailman/listinfo/baypiggies
> >


More information about the Baypiggies mailing list