【问题标题】:Python3 wrapping functionsPython3 包装函数
【发布时间】:2020-09-01 11:59:44
【问题描述】:

我无法在任何地方找到我的问题的解决方案,所以也许我可以在这里得到答案。 基本上我尝试用另一个函数打印一个元组,然后尝试将该输出包装在另一个函数中。如果我将文本包装在 Wrapped_string_linesplit() 函数中,它可以完美地工作,但我基本上不希望这样。 也许有人可以解释我的错误在哪里,以及从另一个函数中包装它的好解决方案是什么。提前致谢

工作解决方案:

def wrapped_string_linesplit(arg):
    print('#' * 30)
    for item in arg:
        print(item)
    print('#' * 30)


def welcome_message():
    first = 'Welcome to xyz'.center(30, ' ')
    second = ('#' * 30).center(30, ' ')
    third = 'New Game'.center(30, ' ')
    fourth = 'Load Game'.center(30, ' ')
    fifth = 'About'.center(30, ' ')
    sixth = 'Quit Game'.center(30, ' ')
    return first, second, third, fourth, fifth, sixth

wrapped_string_linesplit(welcome_message())

输出:

##############################
        Welcome to xyz        
##############################
           New Game           
          Load Game           
            About             
          Quit Game           
##############################

然后将代码更改为以下代码,根本不会打印包装而不会出现错误:

def message_wrapper(foo):
    def wrap():
        print('#' * 30)
        foo()
        print('#' * 30)
    return wrap
    

def string_linesplit(arg):
    for item in arg:
        print(item)

message_wrapper(string_linesplit(welcome_message()))

输出:

        Welcome to xyz
##############################
           New Game           
          Load Game           
            About             
          Quit Game           
##############################

下一个完全看不懂,这个抛出Error

foo()

TypeError: 'NoneType' 对象不可调用 在 message_wrapper() 中调用 foo() 时。 为什么它必须有一个返回值才能被另一个函数调用?

def message_wrapper(foo):
    print('#' * 30)
    foo()
    print('#' * 30)


def string_linesplit(arg):
    for item in arg:
        print(item)

message_wrapper(string_linesplit(welcome_message()))

【问题讨论】:

  • 您的代码中没有名为foo() 的方法。您将 foo 声明为 message_wrapper 函数中的变量。 foo 是变量,不能调用变量
  • 在最后的代码 sn-p 中,string_linesplit 不返回任何内容(无)因此,foo 为无,并导致错误:NoneType object is not callable。我觉得你有点复杂了
  • 非常感谢,现在一切都清楚了。这两个答案都导致了一个可能的解决方案。不确定是否仍然过于复杂。

标签: python function error-handling wrapper


【解决方案1】:

我对给定的代码 sn-p 感到困惑,但如果我必须使用给定的代码段并提供上述输出:

def welcome_message():
    first = 'Welcome to xyz'.center(30, ' ')
    second = ('#' * 30).center(30, ' ')
    third = 'New Game'.center(30, ' ')
    fourth = 'Load Game'.center(30, ' ')
    fifth = 'About'.center(30, ' ')
    sixth = 'Quit Game'.center(30, ' ')
    return first, second, third, fourth, fifth, sixth


def string_linesplit(arg):
    for item in arg:
        print(item)

def message_wrapper(foo):
    print('#' * 30)
    string_linesplit(foo())
    print('#' * 30)

message_wrapper(welcome_message)

输出:

##############################
        Welcome to xyz        
##############################
           New Game           
          Load Game           
            About             
          Quit Game           
##############################

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多