Is it possibly to programmatically create object methods in python?
Due to working with the existing structure of a command line parser, I require an arbitrary number (N) of methods, where (N) is established at runtime.
def activatetool_>N<():
print 'activating tool number: >N<'
I managed to get close with:
class TestClass:
def __init__(self, toolcount):
for i in range(toolcount):
exec('def activatetool_{}(): print \'activating tool {}\''.format(i,i)) in globals()
However this defines global functions, not class methods. By design of the existing code I am working with, I need to be able to call them in the following form:
obj=TestClass(5)
obj.activatetool3()
obj.activatetool1()
Clarification: due to the structure of the existing parser I am working with, solutions that are refactoring to the form of obj.activatetool(N)
are not workable.