简体   繁体   English

跨函数传递值不会产生 output

[英]passing values across functions yields no output

I have the code below where I use python package click to fetch some input from user.我有下面的代码,我使用 python package click从用户那里获取一些输入。 I then pass the user input to a function that has code to load a pre-trained model. I return a list of values that I pass to a second function that generates text using the model and other values.然后,我将用户输入传递给 function,它具有加载预训练 model 的代码。我返回一个值列表,传递给第二个 function,后者使用 model 和其他值生成文本。 However the values aren't passed from first function to the second because when I try to print the list I get nothing.但是,值不会从第一个 function 传递到第二个,因为当我尝试打印列表时,我什么也得不到。 Could someone point out what I'm doing wrong, thanks a lot!!谁能指出我做错了什么,非常感谢!!

@click.argument('email_template', nargs=1)
def load_model(email_template):
    ## code block here
    list1 = [email_template, value1, value2]
    return list1


def generate_text(value2):
    # code block here
    return result

if __name__ == '__main__':
    list1 = load_model()
    list2 = generate_text(list1)
    print(list2)

You are missing a @click.command() decorator.您缺少@click.command()装饰器。 It is not enough to use @click.argument() , then expect this to work.仅使用@click.argument()是不够的,然后期望它起作用。 The @click.command() -decorated function becomes the entry point of your script, and should not be seen as something that'll return the user options. @click.command()修饰的 function 成为脚本的入口点,不应被视为会返回用户选项的东西。

Also, if email_template is the only option your script takes and it expects just one value, there is no point in using nargs=1 .此外,如果email_template是您的脚本采用的唯一选项并且它只需要一个值,则使用nargs=1没有意义。

So do this:所以这样做:

import click

@click.command()
@click.argument('email_template')
def load_model(email_template):
    ## code block here
    # This is your *main script function*.

    list1 = [email_template, value1, value2]

    # don't return, continue the work you need doing from here
    list2 = text_generator(list1)
    print(list2)

def generate_text(result):
    # code block here
    return value2

if __name__ == '__main__':
    load_model()

When load_model exits, your script exits.load_model退出时,您的脚本也会退出。

Also, rather than use print() , consider using click.echo() , especially when you need to print text that uses non-ASCII characters and needs to work on a variety of platforms, or if you want to include ANSI colors in your output.此外,不要使用print() ,而应考虑使用click.echo() ,尤其是当您需要打印使用非 ASCII 字符并需要在各种平台上工作的文本时,或者如果您想在您的文件中包含 ANSI colors output。

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

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