简体   繁体   English

如何在Python中组合多个函数的输出(字符串)?

[英]How can I combine multiple function's output(strings) in Python?

I want to combine strings returned by multiple functions conditionally; 我想有条件地合并多个函数返回的字符串; somehow it's not working: 某种程度上它不起作用:

file1.py file1.py

CALL_FUNCTION = {
    ‘function1’: function1
}

def function1():
    return “hello”

def function2():
    return “world”

file2.py file2.py

from file1 import CALL_FUNCTION

def final_function(condition):
    final_string=CALL_FUNCTION[‘function1’] #Calls function1 from file1.py
    if(condition)
        from file1 import function2
        final_string += function2() 

    return final_string

final_function(true) #This does not work; no error. Expected output is “hello world” when condition is true

Your code had lots of minor bugs. 您的代码有很多小错误。 I suspect they all happened because you copied the code by manually retyping it, please use the copy-paste function in future problems. 我怀疑它们都是因为您通过手动重新键入代码而复制了代码,请在将来出现问题时使用复制粘贴功能。

Here is the corrected version of your code: 这是您的代码的更正版本:

file1.py file1.py

def function1():
    return "hello"

def function2():
    return "world"

CALL_FUNCTION = {
    'function1': function1
}

file2.py file2.py

from file1 import CALL_FUNCTION

def final_function(condition):
    final_string=CALL_FUNCTION['function1']() #Calls function1 from file1.py
    if condition:
        from file1 import function2
        final_string += function2() 

    return final_string

final_function(True) #This does not work; no error. Expected output is “hello world” when condition is true

But after that, everything is working. 但是之后,一切正常。 The functions get called, as expected, the function returns 'helloworld', as expected, and nothing gets printed, as expected. 如预期那样调用函数,该函数按预期返回“ helloworld”,并且按预期不打印任何内容。

final_function(True) would only print something in interactive mode, in batch mode you need to add print() to actually see the result. final_function(True)仅在交互模式下打印某些内容,在批处理模式下,您需要添加print()才能实际看到结果。

print(final_function(True))

Output: 输出:

helloworld 你好,世界

If you want to return output of different functions as per "condition", you may import both functions and run each in if-else clause. 如果要根据“条件”返回不同函数的输出,则可以导入两个函数并在if-else子句中运行每个函数。 for example: 例如:

In file1.py 在file1.py中

def func1():
    return "hello"

In file2.py 在file2.py中

def func2():
    return "world"

Then in separate file3.py 然后在单独的file3.py中

from file1 import func1
from file2 import func2

def func3(cond):
  out = func1()
  if cond:
     out+=' '+func2()

  return out

if __name__ =='__main__':
    print(func3(True))

Output : 输出:

hello world

I'm surprised you're saying that there's no error when you're running this code as there's a couple bugs in your code, so I'm suspecting that this isn't the exact code that you've been running. 令您惊讶的是,您在运行此代码时没有错误,因为您的代码中存在几个错误,因此我怀疑这与您所运行的代码不完全相同。 First of all, make sure to define CALL_FUNCTION below function1 . 首先,请务必确定CALL_FUNCTION以下function1 Secondly, there's a couple of syntax errors - use True rather than true as well as propper string quotes ( ' or " ). 其次,存在一些语法错误-使用True而不是true以及适当的字符串引号( '" )。

But I believe that the main issue here is that you're never actually calling function1 and you're trying to concat a function object final_string to a string which is a result of calling function2 . 但是我相信这里的主要问题是,您实际上从未真正调用过 function1 ,而是试图将函数对象final_string到字符串,这是调用function2的结果。 In order to fix that you'd need to replace: 为了解决该问题,您需要更换:

final_string=CALL_FUNCTION['function1']

with

final_string=CALL_FUNCTION['function1']()

This ensures you call function1 and save its result to final_string instead of saving the function object. 这样可以确保您调用function1并将其结果保存到final_string而不是保存function对象。

PS: I recommend going over PEP 8 style guide in order to make your code cleaner and easier to read for others in the future. PS:我建议翻阅PEP 8样式指南 ,以使您的代码更简洁 ,将来更易于他人阅读。

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

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