简体   繁体   English

在其他函数中调用函数?

[英]Calling functions within other functions?

I'm really stuck with this, I need to call a function within another function changing the argument while inside but it keeps me saying 'float' object is not callable. 我真的很坚持这一点,我需要在另一个函数中调用一个函数,以在内部更改参数,但这让我说“ float”对象不可调用。

for example: 例如:

def functioncall(f,epsilon):
   guess = 1.0
   for i in range(100):
      if f(guess) - guess < epsilon:
        return guess
      else:
        guess = f(guess)

gives me 给我

File "C:/Users/Apoo/Dropbox/6.00x/midTermQuiz1/problem8.py", line 16, in functioncall
    if f(guess) - guess < epsilon:
TypeError: 'float' object is not callable

I understood that doing so I'm calling the value of the function f, which is a float in my case, but how can I change that? 我知道这样做是在调用函数f的值,在我看来,这是一个浮点数,但是我该如何更改呢?

You are actually passing the return value of the function f instead of the function object itself. 实际上,您正在传递函数f的返回值,而不是函数对象本身。

use just : functioncall(f,0.001) when calling functioncall 在调用functioncall时只使用: functioncall(f,0.001)

if you do something like functioncall(f(<int here>),0.001) , then it sends the value returned by f(<int here>) as the first argument and calling (guess) on it raised the Error. 如果您执行类似functioncall(f(<int here>),0.001) ,则它将f(<int here>)返回的值作为第一个参数发送,并对其进行调用(guess)会引发Error。

For your code as written to work, the first argument passed to functioncall must be a function or other callable object. 为了使您的代码能够正常工作,传递给functioncall的第一个参数必须是一个函数或其他可调用对象。 You are passing an int or float. 您正在传递int或float。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM