Python string formatting: % vs concatenation

前端 未结 4 997
不思量自难忘°
不思量自难忘° 2020-12-09 12:05

I\'m developing an application in which I perform some requests to get an object id. After each one of them, I call a method (get_actor_info()) passing this id

4条回答
  •  鱼传尺愫
    2020-12-09 12:59

    Python 3.6 will introduce yet another option:

    ACTOR_CACHE_KEY_PREFIX = 'actor_'
    
    def get_actor_info(actor_id):
        cache_key = f'{ACTOR_CACHE_KEY_PREFIX}{actor_id}'
    

    Performance should be comparable to '{}{}'.format(ACTOR_CACHE_KEY_PREFIX, actor_id), but is arguably more readable.

提交回复
热议问题