Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3:如何从另一个文件调用函数并将参数传递给该函数?_Python_Function_Python 3.x - Fatal编程技术网

Python 3:如何从另一个文件调用函数并将参数传递给该函数?

Python 3:如何从另一个文件调用函数并将参数传递给该函数?,python,function,python-3.x,Python,Function,Python 3.x,我想从另一个文件调用函数,并将参数从当前文件传递到该文件。在下面的示例中,在文件bye.py中,我想从“hi.py”文件调用函数“me”,然后将“再见”字符串传递给函数“me”。怎么做?谢谢:) 我有hi.py文件 def me(string): print(string) me('hello') 再见 from hi import me me('goodbye') 我得到的是: hello goodbye 我想要的是: goodbye 通常,在创建要导入的文件时,如果从另一个文

我想从另一个文件调用函数,并将参数从当前文件传递到该文件。在下面的示例中,在文件bye.py中,我想从“hi.py”文件调用函数“me”,然后将“再见”字符串传递给函数“me”。怎么做?谢谢:)

我有hi.py文件

def me(string):
    print(string)
me('hello')
再见

from hi import me
me('goodbye')
我得到的是:

hello
goodbye
我想要的是:

goodbye

通常,在创建要导入的文件时,如果从另一个文件导入文件,则必须使用
if _uname\u=='\u u main\u u'
,其计算结果为false。因此,您的hi.py可能看起来是:

def me(string):
    print(string)

if __name__ == '__main__':
    # Do some local work which should not be reflected while importing this file to another module.
    me('hello')
从hi.py中删除
me('hello')