我的Python项目能够在两个不同的目标上执行操作,我们称它们为SF和LA。哪个更好的方法来完成这个?作为常量或字符串比较枚举
选项A:
destinations.py
LA = 1
SF = 2
example_operation.py
import destinations
run_operation(destination=destinations.LA)
def run_operation(destination):
assert destination in [destinations.LA, destinations.SF]
...
OR
选项B:
example_operation.py
run_operation(destination='LA')
def run_operation(destination):
assert destination in ['LA', 'SF']
...
我知道我也可以使用字典或许多其他方法来实现这一点。我想知道哪些是宣布和验证这些的最佳做法。
对于成员测试,一个集合更有效 - 'DESTINATIONS = set(('LA','SF'))'然后'在DESTINATIONS中声明目的地。但是你还需要这些值吗? – jonrsharpe 2015-01-20 21:03:46
这是非常主观的,并不会真的产生任何好的答案。你可以将两者结合起来,做'LA ='LA'和'SF''SF'。 – poke 2015-01-20 21:14:00
@jonrsharpe我不太关心表现,但谢谢。 “目标”用于稍后确定加载地址。 – disambiguator 2015-01-20 21:30:07