Assign a print statement to a variable in a function in Python 2.7

前端 未结 3 916
生来不讨喜
生来不讨喜 2021-01-29 12:38

I\'m trying to assign a print statement to a variable in a function:

def namer(fn, ln=\'Smith\'):
    # return value, default value
    r = print \"Your name is          


        
3条回答
  •  遇见更好的自我
    2021-01-29 13:18

    As you noted, in Python2.x, print is a statement. A statement is not an object, you cannot assign it to anything, you can just execute it.

    Why would you want to return the print statement? Why not the string to be printed or a function that, when called would execute the print statement?

提交回复
热议问题