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

Steve Hindle mech422 at gmail.com
Sat Jan 1 00:39:03 CET 2011


I'm half-asleep so this is prolly all wrong but here's a shot at it...
I cant' remember if random function calls are actually invoked during
class definition or not...
if they are, then they should get a cls as the first parm - which
would be your context as no
objects have been created yet...

If they're not, I would guess you'd have to move them outside the
class definition (maybe right after it)
there they will definately get called right after the class
definition. this would give you a syntax like
is_a(Foo, A) - again allowing you to capture the class.

Since this is working on classes, not objects - I'm not sure what your
approach gives you
over standard syntax where base classes are listed in the class
signature ?  The only
'context' you'd be capturing is the relationship between
classes...is-a is a standard base class,
and part-of sounds like a mixin - again, a base class ?

Steve



On Fri, Dec 31, 2010 at 2:23 PM, Bill Janssen <janssen at parc.com> wrote:
> 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
>> >
> _______________________________________________
> 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