简体   繁体   English

使用函数作为输入

[英]Using functions as input

I've written a program (A) that takes, as input, the names of functions I wrote in a separate program (B). 我已经编写了一个程序(A),该程序以我在单独的程序(B)中编写的功能名称作为输入。 I want to use these functions in program (A), so I am trying to run (A) by doing this: A(f1, f2, f3, f4) 我想在程序(A)中使用这些功能,所以我试图通过以下方式运行(A):A(f1,f2,f3,f4)

At the top of (A), I imported program (B) using import B. In (A) there is just one function (excluding main) that accepts four inputs (f1, f2, f3, f4), and then uses them, like this: 在(A)的顶部,我使用导入B导入了程序(B)。在(A)中,只有一个函数(不包括main)接受四个输入(f1,f2,f3,f4),然后使用它们,像这样:

    for i in range(x, y):
       z = B.f1(i)
       u = B.f2(i)
       ...
    ...

The trouble is, when I try to run A(f1, f2, f3, f4), I get the error 问题是,当我尝试运行A(f1,f2,f3,f4)时,出现错误

Traceback (most recent call last):
   File "<pyshell#0>", line 1, in <module>
      A(f1, f2, f3, f4)
NameError: name 'f1' is not defined

I see that python is not recognizing the functions in (B) as input in (A), but I don't know why or how else I can connect the two programs. 我看到python无法将(B)中的功能识别为(A)中的输入,但是我不知道为什么或如何可以连接这两个程序。

Update: Program A 更新:程序A

def A(f1, f2, f3, f4) :

   x = 0
   y = 10

   for i in range(x, y):
       x = B.f1(i) //returns float
       plt.plot(i, x)
   ... 

Based on a literal reading of your question, if you imported B via 根据您对问题的字面理解,如果您通过

import B

then every reference to functions, variables, classes etc defined in B must be done in the form B.func1 etc. 那么对B中定义的函数,变量,类等的所有引用都必须以B.func1等形式B.func1

Your error message clearly says that you're trying to do A(f1, f2, f3, f4) . 您的错误消息清楚地表明您正在尝试执行A(f1, f2, f3, f4) This should be A(B.f1, B.f2, B.f3, B.f4) 这应该是A(B.f1, B.f2, B.f3, B.f4)

EDIT Judging from your updated question, I'm guessing you want something like: 编辑从您更新的问题来看,我猜您想要这样的东西:

import B

def A(input_function1, input_function2, input_function3, input_function4) :
    x = 0
    y = 10

    for i in range(x, y): #btw, you don't need the x value here if it's 0
        x = input_function1(i) //returns float #I removed the 'B.'
        plt.plot(i, x)
    # Other stuff here

if __name__=='__main__':
    A(B.f1, B.f2, B.f3, B.f4)
    # or alternatively A(B.f78, B.f21, B.f1, B.f90) or whatever

Or alternatively: 或者:

from B import f1, f2, f3, f4

def A(f1, f2, f3, f4) :
    # Stuff here

if __name__=='__main__':
    A(f1, f2, f3, f4) # Explicit imports mean that the 'B.' prefix is unnecessary

To extend what @zehnpaard has already suggested, if you want to call the B function in A() using f1...f4 as arguments, you can alternatively use getattr() , like this: 为了扩展@zehnpaard已经建议的内容,如果要使用f1 ... f4作为参数在A()中调用B函数,则可以选择使用getattr() ,如下所示:

...
def A(f1, f2, f3, f4) :

   x = 0
   y = 10

   for i in range(x, y):
       x = getattr(B, f1)(i)
       plt.plot(i, x)
   ... 

getattr(B, f1) will return the function as in B.f1 , and you call the function and pass the argument i with (i) , just like: getattr(module, function)(*args) . getattr(B, f1)将返回B.f1的函数,然后调用该函数并通过(i)传递参数i ,就像: getattr(module, function)(*args)

For example: getattr(plt, 'plot')(i, x) = plt.plot(i, x) 例如: getattr(plt, 'plot')(i, x) = plt.plot(i, x)

Pay attention that you pass plot as string 'plot' , so when you're trying to call function A() , you will do: 请注意,将plot作为字符串'plot'传递,因此,当您尝试调用函数A()时 ,您将执行以下操作:

if __name__=='__main__':
    A('f62', 'f22', 'f2323', 'f100')

As a side note: if the function you pass is not in B , it will raise AttributeError . 附带说明:如果您传递的函数不在B中 ,它将引发AttributeError

Hope this helps. 希望这可以帮助。

Sounds like you have the following defined in your interactive session 听起来您在交互式会话中定义了以下内容

import B

def A(f1, f2, f3, f4):

    for i in range(10):
        plt.plot(i, f1(i))

And the user should call it by doing: 用户应该通过执行以下操作来调用它:

>>> A(B.f1, B.f49, B.f100, B.f42)

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

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