是否有理由在.format中声明变量?

iha*_*dea 3 python

我正在网上关注某个Python教程,导师正在讲授这个.format()方法.

例如:

print("{}, {} and {} are colors.".format("Red", "Blue", "Green"))
Run Code Online (Sandbox Code Playgroud)

将输出

Red, Blue and Green are colors.
Run Code Online (Sandbox Code Playgroud)

也可以使用索引(也许这不是正确的措辞):

print("{0}, {1} and {2} are colors.".format("Red", "Blue", "Green"))
Run Code Online (Sandbox Code Playgroud)

这将输出相同的东西.

然而,他然后提出了另一种声明变量的方法(再次,可能这不是正确的措辞),如下所示:

print("{r}, {b} and {g} are colors.".format(r="Red", b="Blue", g="Green"))
Run Code Online (Sandbox Code Playgroud)

这再次输出相同的结果.

有没有使用像变量的任何优势r,bg里面.format()的方法?

我想到的一件事是我可以在程序中使用这些变量,但如果我尝试使用它们,我会得到一个NameError: name 'r' is not defined.

tim*_*geb 7

有没有使用像变量的任何优势r,b并且g在.format()方法中?

当您需要多次引用同一个对象时,使用关键字参数尤其有用.

演示:

>>> class Signal: 
...:     status = 'on' 
...:     color = 'red' 
...:                                                                                                                   
>>> 'the signal is {sig.status} and the color is {sig.color}'.format(sig=Signal)                                       
the signal is on and the color is red
Run Code Online (Sandbox Code Playgroud)

你可以实现同样的目标

>>> 'the signal is {0.status} on the color is {0.color}'.format(Signal)                                         
the signal is on on the color is red
Run Code Online (Sandbox Code Playgroud)

但是使用名称可以使字符串更容易解释为读取代码的人.

此外,关键字参数可以按任何顺序传递,而您必须确保以正确的顺序传递位置参数.这是另一个希望证明关键字参数的可用性优势的例子.

>>> class Fighter: 
...:     def __init__(self, name, attack, defense): 
...:         self.name = name 
...:         self.attack = attack 
...:         self.defense = defense                                                                                                                          
>>>                                                                                                                                                          
>>> Bob = Fighter('Bob', 100, 80)                                                                                                                            
>>> Tom = Fighter('Tom', 80, 90)                                                                                                                             
>>> template = 'Attacker {attacker.name} attempts hit at {defender.name} with {attacker.attack} (ATK) against {defender.defense} (DEF)'                                  
>>>                                                                                                                                                          
>>> template.format(attacker=Bob, defender=Tom)                                                                                                              
'Attacker Bob attempts hit at Tom with 100 (ATK) against 90 (DEF)'
>>> template.format(defender=Tom, attacker=Bob)                                                                                                              
'Attacker Bob attempts hit at Tom with 100 (ATK) against 90 (DEF)'
Run Code Online (Sandbox Code Playgroud)