For finding the default value of a parameter programmatically, we can use the signature function in the inspect
module to get a signature object. Then using the parameters
on the object, we can obtain the parameters of the function, and access the default values through indexing.
import inspect
sig = inspect.signature(KNeighborsClassifier)
parameter_name, value = str(sig.parameters["n_neighbors"]).split("=")
print(f"The default value of {parameter_name} is {value}.")
>>> The default value of n_neighbors is 5.