I have a python3 object (class instance), generated by an
myObj = myClass()
line. I have also a myMethodName string:
myMethodName = "exampleName"
. How can I call a method of myObj named by myMethodName? Thus, in this case, I want to call myObj.exampleName, which is coming from like a getAttr(myObj, myMethodName).
Unfortunately, a naive solution refered by the python docs (for example, here) gives my only a KeyError or AttributeError.
This fails on that myObj doesn't have a method method:
method = getAttr(myObj, myMethodName)
myObj.method(param)
This fails on that myMethodName has more of fewer arguments:
method = getAttr(myObj, myMethodName)
method(myObj, param)
Calling simply
method = getAttr(myObj, myMethodName)
method(param)
which would be the most logical, gives
TypeError: exampleName() takes 1 positional argument but 2 were given
So, how can I do this?
I use python 3.6, if it matters.
Aucun commentaire:
Enregistrer un commentaire