Twice bound method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carlos

    #1

    Twice bound method

    Hi!

    I want to instrumentate a class with a number of getter/setters.
    Each pair of getter/setter must keep it's own state gsState but also
    access to the state iState of instances of the instrumentated class.
    For example:

    class GetterSetter:
    def __init__(gsInst ance, gsState):
    ....
    def get(gsInstance, iInstance, attr):
    ....
    def set(gsInstance, iInstance, attr, value):
    ....

    class Instrumentated:
    def __init__(iInsta nce, iState):
    ....

    getterSetter = GetterSetter(gs State1)
    Instrumentated. getter1 = getterSetter.ge t
    Instrumentated. setter1 = getterSetter.se t

    getterSetter = GetterSetter(gs State2)
    Instrumentated. getter2 = getterSetter.ge t
    Instrumentated. setter2 = getterSetter.se t

    instrumentated = Instrumentated( ...)
    instrumentated. getter1("x")
    instrumentated. setter2("x", 5)

    At first sight I thought that the above would work fine
    as getterSetter.ge t would bind the getter to the GetterSetter
    instance and then instrumented.ge tter1 would bind the already
    bound getter to the Instrumentated instance, so at the end
    an invocation like instrumentated. getter1("x") would be calling the
    original getter passing a GetterInstance as first implicit
    argument, an Instrumented instance as a second one and "x"
    as the third -explicit- one. Well, the fact is that the getter
    is only bound to the last instance, there are no nested bindings.

    Another solution could come from the use of function nested
    lexical scopes and closures, with a factory function which
    takes the gsState as argument and produces a getter (or setter)
    function taking an iState as first argument and the attribute as
    second one. Then the class can be instrumentated with the generated
    getter (or setter) which keeps the gsState captured within its closure.
    For example:

    def getterGen(gsSta te):
    def getter(iState, attr):
    ....
    return getter

    Instrumentated. getter1 = getterGen(gsSta te1)
    Instrumentated. getter2 = getterGen(gsSta te2)

    Do you know of another -elegant- solution for the above problem?
    Is there any way to get the nested method binding behaviour that
    the first failed attempt required?

    Thank you in advance.
    Regards,
    Carlos

Working...