0

How a(b) works in this code?

def here(a, b):
    print('Hello')
    a(b)

Does a become the function? a and b are parameters of the function here. What is the concept of a(b).

def near(f):
    print(f)
    
here(near, 'Where?')
Gigi09
  • 11
  • 1
    Does this answer your question? [Python - Passing a function into another function](https://stackoverflow.com/questions/1349332/python-passing-a-function-into-another-function) – Vladimir Fokow Sep 13 '22 at 13:44
  • `a` must already be a function, or at least a callable object- otherwise an exception will be raised – Chris_Rands Sep 13 '22 at 13:46
  • `a` doesn't "become" a function. `a` must be an object that implements the `__call__()` function (such as a declared func) or you will get an exception when you call it as `a(b)`. – vaizki Sep 13 '22 at 13:46
  • In Python, everything is an object. Functions are in fact objects, so you can pass them as argument of other functions – Achille G Sep 13 '22 at 13:46

1 Answers1

1
def here(a, b):
    print('Hello')
    a(b)
def near(f):
    print(f)
    
here(near, 'Where?')

What is the concept(...)?

This code does rely on that functions in python are first-class citizens, Who Is First-Class Citizen In Programming World? gives following claim

A programming language is said to have first-class functions when functions in that language are treated like any other variable. For example, a function can be passed as an argument to other functions, can be returned by another function, and can be assigned as a value to a variable.

Daweo
  • 31,313
  • 3
  • 12
  • 25