Function parameter passing in Python

The following code represents an attempt at a minimal, reproducible example which compiles and runs as expected.

import numpy as np
from sklearn import  model_selection as skms

N = 20

ftr = np.linspace(-10,10,num=N) # ftr values
tgt = 2*ftr**2 -3 + np.random.uniform(-2,2,N) # tgt =func(ftr)

(train_ftr, test_ftr, train_tgt, test_tgt) = skms.train_test_split(ftr, tgt, 
test_size = N//2)

model_one = np.poly1d(np.polyfit(train_ftr, train_tgt, 1))
preds_one = model_one(test_ftr)

Where the following are of type numpy ndarrays.

train_ftr
test_ftr
train_tgt
test_tgt

My question relates to the output of the last line preds_one wrt the second last line model_one. In the last line, how can you pass a parameter test_ftr to the function model_one defined in the second last line when model_one is a composite function? That is, what is test_ftr actually being passed to?

Answer

np.poly1d returns a callable object. That is, it can be called like a function (i.e. it is a function). Objects in Python are callable if they have a __call__() function.

By calling model_one(test_ftr), you are actually calling model_one.__call__(test_ftr), which is a method that this object has.

Every function in Python is an object with a __call__ method.