Just use the name of the function as an argument for another function. Methods and functions are objects in Python, just like anything else, and we can pass them around the way do variables.
Functions (and methods) are first-class objects in Python. So you can pass a function as an argument into another one in python.
Example passing function as an argument in Python
Simple example code.
Passing bar() funciton as a argument into a foo() funciton.
def foo(f): print("Running parameter f().") f() def bar(): print("In bar().") foo(bar)
def up_text(text): return text.upper() def low_text(text): return text.lower() def greet(func): # storing the function in a variable greeting = func("Hello, Function") print(greeting) greet(low_text) greet(up_text)
Do comment if you have any doubts and suggestions on this Python function tutorial.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.