Ad

Simple Function vs Lambda

  • Code now defines a standard function with parameters, directly returning the formatted string based on the inputs.

  • Code is now called with a single set of parentheses, and arguments are passed in the standard way.

  • Readability dramatically improved

Testing

  • In the original setup, testing required understanding that the function produced a callable object, resulting in a syntax that could be confusing (Greeting(args)()).

  • Tests now call Greeting like any other function.

  • Anyone reading the tests can now quickly understand how the function is supposed to be used

Code
Diff
  • def Greeting(name, rank=None, formal=False):
        greeting_style = "Hello" if formal else "Hey"
    
        rank_part = f"{rank} " if rank and formal else ""
    
        punctuation = '.' if formal else '!'
    
        return f"{greeting_style}, {rank_part}{name}{punctuation}"
    
    • Greeting=lambda name,rank=None,formal=False: lambda:f'He{["y","llo"][formal]}, {[str(rank)+" ",""][not rank or not formal]}{name}{chr(33+formal*13)}'
    • def Greeting(name, rank=None, formal=False):
    • greeting_style = "Hello" if formal else "Hey"
    • rank_part = f"{rank} " if rank and formal else ""
    • punctuation = '.' if formal else '!'
    • return f"{greeting_style}, {rank_part}{name}{punctuation}"