Posts

Showing posts with the label Python - Function as Type

Python - Function as Type, Parameter and Result of Another Function

Image
Function as type In Python, a function represents a separate type. Therefore, we can assign a function to a variable and then call this function using the variable. For example: def print_hello(): print( "Hello World" ) def print_goodbye(): print( "Good Bye" )   message = print_hello message()       # Hello World message = print_goodbye message()       # Good Bye In this case, the variable message is assigned one of the functions. First, the function print_hello(): is passed to it message = print_hello After this, the message variable will point to this function, that is, it will actually represent the print_hello function. And this means that we can call the message variable like a regular function: message()       # Hello World This will actually cause the print_hello function to be executed and the string "Hello World" will be printed to the console. Then in a similar way we can pass the variable message another function and call it. In a similar way,