python string format calling a function

前端 未结 5 789
情书的邮戳
情书的邮戳 2020-12-06 04:16

Is there a way to format with the new format syntax a string from a function call? for example:

\"my request url was {0.get_full_path()}\".format(request)
         


        
5条回答
  •  一个人的身影
    2020-12-06 05:14

    Python does not directly support variable interpolation. This means that it lacks certain functionality (namely, function calling in strings) which other languages support.

    So, there isn't really anything to say here other than no, you can't do that. That's just not how Python's formatting syntax works.

    The best you have is this:

    "my request url was {0}".format(request.get_full_path())
    

提交回复
热议问题