Pointer to Python classes and methods -
i have function pointer dictionary points different class methods. best method of doing it?
class a(): command_map = { "call func1 of object1": ("1", func1) "call func1 of object2": ("2", func1) } def __init__(self, object1, object2): self.o1 = object1 self.o2 = object2 def call(self, cmd): t = self.command_map[cmd] target = t[0] func = t[1] if target == "1": resp = self.o1.func() elif target == "2": resp = self.o2.func() else: self._logger.warn("unknown target %r\n" % target)
in case eclipse highlights func variable saying unused. how proceed? want call funca() of objecta or funcb() of objectb dependent on input values.
is there way self.o1 put "command-map"?
change command_map
hold function names instead of actual 'pointers'.
command_map = { 'call func1 of object1': ('1', 'func1'), 'call func1 of object2': ('2', 'func1'), }
then can define call
method this:
def call(self, cmd): t = a.command_map[cmd] # command_map class attribute, not instance attribute if t[0] == '1': o = self.o1 elif: o = self.o2 else: self._logger.warn("unknown target %r\n" % target) return f = getattr(o, t[1]) # method f()
Comments
Post a Comment