如果需要导入,则在另一个函数中的函数参数

时间:2016-03-22 06:33:51

标签: python python-2.7

我在stackoverflow上找到了这个脚本,这有助于我将参数从一个函数传递到另一个函数:

def perform( fun, fun2, *args ):
  fun(*args)
  fun2(*args)

def action1(filename, destination):
  print filename, destination

def action2(filename, destination):
  print 'help me find my {}'.format(filename)
  print 'it is located at {}'.format(destination)

perform(action1, action2, 'text.csv','/User/username/Desktop')

结果:

text.csv /User/username/Desktop
help me find my text.csv
it is located at /User/username/Desktop

但是......如果action1action2在另一个python .py文件中怎么办?我可以导入它们,但不确定如何使它的行为与上面相同。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

from usermodule1 import action1, action2  from usermodule2 import perform

perform(action1, action2)

usermodule1.py - .py file with defined action1, action2 functions
usermodule2.py - .py file with defined perform funtion