In python I can do:
exec(code_string, globals(), some_object.__dict__)
to add a method to an object. Is it possible to add a static method to a class in some sort of similar fashion? Like:
exec(code_string, globals(), ClassName.__dict__)
So that I could then statically call the method:
ClassName.some_static_method()
What I'm trying to do is add new staticmethods during runtime given some python code defining the method. i.e. if I was given:
code_string = '''
@staticmethod
def test():
return 'blah'
'''
how can I create and instantiate this into a class so that I could call it?
Hopefully I was clear enough, thank you!
EDIT: working example of adding a function to an object:
class TestObject(object):
pass
code_string = '''
def test():
return 'blah'
'''
t = TestObject()
exec(code_string, globals(), t.__dict__)