J
Josh English
I may not be using the language properly, but I suspect what I have is a
binding problem and I'm wondering if there's a better solution than
what I've come up with.
I'm working on a Stack based language that can import methods from
predefined libraries. Here's what I mean.
-------
class S:
def __init__(self,value):
self.value = value
def __repr__(self):
return '<%s>' % (self.__class__.__name__)
class Library:
### this method refers to 'self.s' which it isn't
### an attribute or method
def do_this(self):
self.s.value = 'this'
class Runner:
def __init__(self):
self.s = S('whock')
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__,self.S)
def importmethod(self,obj):
#setattr(self,obj..__name__,obj.do_this)
self.do_this = obj.do_this
r = Runner()
print dir(r)
r.importmethod(Library)
r.do_this()
-----------
This basically all I want to do, but the last line raises an error
because the Library class has no attribute 's'. Right now my workaround
is to declare the do_this() method of the Library class:
def do_this(self,parent):
parent.s.value = 'this'
and use this in the Runner class:
def importmethod(self,obj):
setattr(self,'do_this',obj.do_this)
#self.do_this =getattr(obj,'do_this')
Is there a better way to do this?
Thanks,
Josh English
english-at-spiritone.com
binding problem and I'm wondering if there's a better solution than
what I've come up with.
I'm working on a Stack based language that can import methods from
predefined libraries. Here's what I mean.
-------
class S:
def __init__(self,value):
self.value = value
def __repr__(self):
return '<%s>' % (self.__class__.__name__)
class Library:
### this method refers to 'self.s' which it isn't
### an attribute or method
def do_this(self):
self.s.value = 'this'
class Runner:
def __init__(self):
self.s = S('whock')
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__,self.S)
def importmethod(self,obj):
#setattr(self,obj..__name__,obj.do_this)
self.do_this = obj.do_this
r = Runner()
print dir(r)
r.importmethod(Library)
r.do_this()
-----------
This basically all I want to do, but the last line raises an error
because the Library class has no attribute 's'. Right now my workaround
is to declare the do_this() method of the Library class:
def do_this(self,parent):
parent.s.value = 'this'
and use this in the Runner class:
def importmethod(self,obj):
setattr(self,'do_this',obj.do_this)
#self.do_this =getattr(obj,'do_this')
Is there a better way to do this?
Thanks,
Josh English
english-at-spiritone.com