简体   繁体   English

使用另一个 Python 文件中的参数调用 function

[英]Call function with argument in another Python file

I want to print a value of returned variable (text) with functions below in a file named words.py我想在名为 words.py 的文件中使用以下函数打印返回变量(文本)的值

def get_text(text):
    match = gettext_pattern.search(text)
    if match:
        return match.group(1).strip()
    match = undersc_pattern.search(text)
    if match:
        return match.group(3).strip()
    
    return text

def dump_text(text):
    out1 = strip_i18n(text)
    
    print(out1)

Then I call it in another python file named out.py然后我在另一个名为 out.py 的 python 文件中调用它

from words import *

dump_text(text)

But then I get this output:但后来我得到了这个 output:

name 'text' is not defined名称“文本”未定义

Question : How to solve this problem?问题:如何解决这个问题? How can I print "out1" variable value?如何打印“out1”变量值?

In your file 'out.py' you're using variable named test which is not defined, do any of the following way.在您的文件“out.py”中,您使用的是未定义的名为 test 的变量,请执行以下任一方式。

Either define variable named 'text' and assigned some data (string).定义名为“文本”的变量并分配一些数据(字符串)。

from words import *

test = 'sample text string'
dump_text(text)

or argument passed should be string.或传递的参数应该是字符串。

from words import *

dump_text('text')

NOTE: It will still raise error(s) after this as there are many not defined variable and functions in words.py file like strip_i18n, gettext_pattern, etc. but your basic problem will be solved by this.注意:它仍然会在此之后引发错误,因为 words.py 文件中有许多未定义的变量和函数,如 strip_i18n、gettext_pattern 等,但您的基本问题将由此解决。

In out.py file, you haven't defined any variable called text.在 out.py 文件中,您还没有定义任何名为 text 的变量。 Also you can't access a variable given contained in a function in another file.此外,您无法访问另一个文件中 function 中给定的变量。

Hence, declare a variable text in out.py因此,在 out.py 中声明一个变量 text

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

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