Can methods have methods in Python?

this may seem a little strange, but it would be convenient for me to finish some code.

Since Python methods are the objects themselves, can a method have its own method? That is, if I wanted to do the following (ignoring the syntax):

def methodCaller(someArgs, methodB): # some stuff here . . . variableWithAGoodName = methodB() jonSkeet = methodB.methodC(variableWithAGoodName) return jonSkeet 

It would be possible? My guess is not, but if the methods are just objects, shouldn't it be somehow?

Thank you very much!

EDIT: I think that as published, I am looking for a high order function.

My question is somewhat academic, because I know that I can reorganize my code to do it completely differently. But be that as it may, I'm experimenting with Python to find out at least its basics. I have not tried this yet, but since I am not familiar with Python, this is possible, just not with this syntax.

Another EDIT: I tried to be funny with my name, but that put the question unclear. I apologize for that. 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 pretend it returns nothing here. # Can this happen? 
+4
source share
2 answers

Yes and no. Obviously, they can have attributes assigned to them that work similarly to methods. In addition, the functions come with methods already installed, for example, the __call__ method, which is called using the function.

However, to add a method to an object, what would you usually do? Subclass the class of the object and add a method. However, if you try to subclass a function

 imports types class F(types.FunctionType): pass 

you will get this error

 type 'function' is not an acceptable base type 

If you want to create a β€œcallable” object that can have methods and use inheritance, try something like this.

 class MyCallable(object): def __init__(self): self.message = "Look ma, I got called!" def __call__(self, *args, **kwargs): self.print_message() def print_message(self): print(self.message) class CallableChild(object): def __call__(self, *args, **kwargs): super(CallableChild, self).__call__(*args, **kwargs) print "...as a child, too!" 
+4
source

Functions in python are first-class objects with methods and attributes.

 def foo(): print("foo") def bar(): print("bar") foo.bar = bar foo.bar() #outputs "bar" foo.baz = "Hello, world!" print(foo.baz) # outputs "Hello, World!" 

Edit:

Since functions are objects of the first class, you can also pass them, like any other variable. You can also write "higher order functions" that are functions of functions (or functions that return functions).

Edit 2:

[Set up "There is no such party as an S-Club party!" ] There is no example, for example, an example with full code!

 def higher_order_function (input_function): input_function.method() def input_function_1 (): print ("exec'ing input_function_1()") def input_function_1_method (): print ("exec'ing input_function_1_method()") input_function_1.method = input_function_1_method higher_order_function(input_function_1) # prints "exec'ing input_function_1_method" 
+6
source

Source: https://habr.com/ru/post/1397102/


All Articles