Search code examples
pythonfunctionerror-handlingwrapper

Python3 wrapping functions


I couldn't find a solution to my questions anywhere so maybe I can get an answer to that here. Basically I tried to printed a tuple with another function and then tried to wrap that output in another one. If I wrap the text in side the wrapped_string_linesplit() function it works perfectly, but I basically don't want it this way. Maybe someone can explain where my mistakes are, and what would be a good solution to wrap it from another function. Thanks in advance

Working Solution:

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())

Output:

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

Then changing the code to the following, doesn't print the wrap at all without an error:

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()))

Output:

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

The next one I don't understand at all, this one throws the Error

foo()

TypeError: 'NoneType' object is not callable when calling foo() inside the message_wrapper(). Why does it have to have a return value to be callable from another function ?

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()))

Solution

  • I am confused with given code snippet, but if I have to work around with given piece of code and give output as mentioned above:

    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)
    

    Output:

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