Code 1 is defining a new function, which calls the print()
function. It would be more pythonic to write it as:
def p(content):
print(content)
lambda
is normally only used for anonymous functions, not named functions.
Case 2 is simply giving another name to the print
function. The two names can be used interchangeably.
The lambda
function only accepts one argument, while the standard print
function alloes multiple positional arguments and named arguments.
So with Code 2 you can write:
p("Hello", "world", end="")
but if you try this with Code 1 you'll get an error because you gave too many arguments to the function.
If you want to define a new function that can take all the arguments that print()
takes, you can use *
.
def p(*args, **kwds):
print(*args, **kwds)
or:
p = lambda *args, **kwds: print(*args, **kwds)
See What does ** (double star/asterisk) and * (star/asterisk) do for parameters?