this may seem a little odd, but it would make for a convenient way for me to finish a bit of code.
Because Python methods are objects themselves, could a method have a method of its own? That is, if I wanted to do the following (ignoring syntax):
def methodCaller(someArgs, methodB):
# some stuff here . . .
variableWithAGoodName = methodB()
jonSkeet = methodB.methodC(variableWithAGoodName)
return jonSkeet
Would it be possible? My guess is no, but if methods are just objects, shouldn’t it be possible somehow?
Thank you so much!
EDIT: I think as has been posted, I am looking for a high-order function.
My question is somewhat academic as I know I could reorganize my code to do this manner of thing totally differently. But, as it is, I am experimenting with Python to learn at least its basics. I haven’t tried this yet, but as I am unfamiliar with Python, it might be possible, just not with this syntax.
Another EDIT: I attempted to be funny with my naming but it made the question unclear. For that I apologize. Here is a better example:
def MethodA(MethodB):
# MethodB is passed as a parameter but is also a method.
# MethodB has a method of its own, somehow, because it is technically still
# an object.
MethodB.MethodC() #Let's pretend it returns nothing here.
# Can this happen?
Yes and no. Obviously they can have attributes assigned to them which work similarly to methods. Further, functions come with methods already attached- for example, the
__call__method, which is called with the function.However, to add a method to an object, what would you typically do? Subclass the object’s class, and add the method. However, if you try to subclass function
you’ll get this error
If you want to make a “callable” object, that can have methods and use inheritance, try something like this.