Python 如果需要导入,则另一个函数中的函数参数
我在stackoverflow上找到了这个脚本,它帮助我将参数从一个函数传递到另一个函数:Python 如果需要导入,则另一个函数中的函数参数,python,python-2.7,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(file
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
但是。。。如果action1
或action2
在另一个python.py文件中怎么办?我可以导入它们,但不确定如何使其与上面的行为相同
感谢您的帮助。
来自usermodule1导入操作1,操作2
从usermodule2导入执行
执行(操作1、操作2)
usermodule1.py-.py文件,包含已定义的action1、action2函数
已定义执行函数的usermodule2.py-.py文件
您所说的“如何使其行为与上述相同”是什么意思?在这个简单的例子中,从另一个模块导入函数实际上不会影响任何事情。这些函数只使用传递给它们的数据,因此它们是否在同一个文件中是无关紧要的。我认为灯泡刚刚切换。谢谢大家! usermodule1.py - .py file with defined action1, action2 functions usermodule2.py - .py file with defined perform funtion