python string format calling a function

前端 未结 5 790
情书的邮戳
情书的邮戳 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:12

    Not sure if you can modify the object, but you could modify or wrap the object to make the functions properties. Then they would look like attributes, and you could do it as

    class WrapperClass(originalRequest):
        @property
        def full_name(self):
            return super(WrapperClass, self).full_name()
    
    "{0.full_name} {0.full_last_name} and my nick name is {0.full_nick_name}".format(user)
    

    which IS legal.

提交回复
热议问题