最初のコメントは、MATLAB関数ハンドルを引数として渡すことができるというアイデアを単純に繰り返しているようです。 2番目のコメントは、これを解釈して、最初のコメント作成者ががにできないと思ったことを意味しています。ラムダまたはのいずれかの関数を直接渡します。。
これを正しく使用すると仮定すると、MATLABの関数ハンドルはをのラムダまたは関数オブジェクトをPythonの入力引数として使用することと機能的に同等です。
関数の最後に()
を追加しないと、関数は実行されず、関数オブジェクトが生成され、別の関数に渡すことができます。
# Function which accepts a function as an input
def evalute(func, val)
# Execute the function that's passed in
return func(val)
# Standard function definition
def square_and_add(x):
return x**2 + 1
# Create a lambda function which does the same thing.
lambda_square_and_add = lambda x: x**2 + 1
# Now pass the function to another function directly
evaluate(square_and_add, 2)
# Or pass a lambda function to the other function
evaluate(lambda_square_and_add, 2)
MATLABでは、MATLABは、あなたが()
を省略した場合でも、関数を実行しようとするため、関数ハンドルを使用するを持っている。
function res = evaluate(func, val)
res = func(val)
end
function y = square_and_add(x)
y = x^2 + 1;
end
%// Will try to execute square_and_add with no inputs resulting in an error
evaluate(square_and_add)
%// Must use a function handle
evaluate(@square_and_add, 2)
回答としてお答えいただきありがとうございます – cbcoutinho