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

Zachary Collins recursive.cookie.jar at gmail.com
Sat Jan 1 09:10:12 CET 2011


In general I think it is not possible without some other explitic
mechanism to determine the class calling context, because it doesn't
fit nicely into the overall system and doesn't have many valid uses.
Besides, you're asking Python to hand you a reference to a class
before it is made, even when it /might not be made/.  Remember that
because of meta classes and decorators and execution order, you can
perfectly execute an entire class and not end up instantiating any
class at all!  Better to solve the problem also in a way that is not
syntatically confusing to a normal python user.  How is it obviously
that invoking a function inside a class definition has side effects to
that class?  Isn't that what meta classes, decorators, and assignment
operators are supposed to be in the first place?

I know it sounds combative, but what I'm getting at is: I think that
in your pursuit of copying another language's syntax, you're avoiding
better python solutions.

2010/12/31 Steve Hindle <mech422 at gmail.com>:
> Bah - told ya I was half asleep... your not gonna get cls since your
> not explicitly passing it...
> Hmm - this one's gonna need more coffee :-)
>
> 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
> _______________________________________________
> 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