Twice bound method

C

Carlos

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__(gsInstance, gsState):
....
def get(gsInstance, iInstance, attr):
....
def set(gsInstance, iInstance, attr, value):
....

class Instrumentated:
def __init__(iInstance, iState):
....

getterSetter = GetterSetter(gsState1)
Instrumentated.getter1 = getterSetter.get
Instrumentated.setter1 = getterSetter.set

getterSetter = GetterSetter(gsState2)
Instrumentated.getter2 = getterSetter.get
Instrumentated.setter2 = getterSetter.set

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

At first sight I thought that the above would work fine
as getterSetter.get would bind the getter to the GetterSetter
instance and then instrumented.getter1 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(gsState):
def getter(iState, attr):
....
return getter

Instrumentated.getter1 = getterGen(gsState1)
Instrumentated.getter2 = getterGen(gsState2)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

need help with properties 4
Code sharing 2
weakrefs and bound methods 18
__getattr__ and recursion ? 2
Enhanced property decorator 4
dynamic setattr 2
functions, classes, bound, unbound? 14
Method chaining 16

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,315
Members
47,996
Latest member
LaurenFola

Latest Threads

Top