首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3包装函数

Python3包装函数
EN

Stack Overflow用户
提问于 2020-09-01 19:59:45
回答 1查看 30关注 0票数 1

我在任何地方都找不到我的问题的解决方案,所以也许我可以在这里得到答案。基本上,我尝试使用另一个函数打印一个元组,然后尝试将该输出包装在另一个函数中。如果我把文本放在wrapped_string_linesplit()函数的旁边,它就能很好地工作,但我基本上不想这样做。也许有人可以解释我的错误在哪里,以及从另一个函数中包装它会是什么好的解决方案。提前感谢

工作解决方案:

代码语言:javascript
复制
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())

输出:

代码语言:javascript
复制
##############################
        Welcome to xyz        
##############################
           New Game           
          Load Game           
            About             
          Quit Game           
##############################

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

代码语言:javascript
复制
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()))

输出:

代码语言:javascript
复制
        Welcome to xyz
##############################
           New Game           
          Load Game           
            About             
          Quit Game           
##############################

下一个我完全不理解的,这个抛出了错误

代码语言:javascript
复制
foo()

TypeError:在message_wrapper()内调用foo()时,无法调用'NoneType‘对象。为什么它必须有一个返回值才能从另一个函数调用?

代码语言:javascript
复制
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()))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-01 20:16:26

我对给定的代码片段感到困惑,但如果我必须处理给定的代码片段并给出如上所述的输出:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
##############################
        Welcome to xyz        
##############################
           New Game           
          Load Game           
            About             
          Quit Game           
##############################
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63687231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档