【发布时间】:2012-03-11 18:54:51
【问题描述】:
如果列表不为空,我正在寻找一种更好的方法来分配具有列表内容的集合,否则应使用另一个列表。
如果可能的话,我想要一个更好的方式来写这个(或者一个为什么这是最好的方式的论据):
if args.onlyTheseServers:
only = set(args.onlyTheseServers)
else:
only = set(availableServers)
【问题讨论】:
-
请记住,官方 Python 风格指南 (PEP 8) 不喜欢你调用你的变量或属性
onlyTheseServers;它会更喜欢only_these_servers之类的。 -
is not None如果您传递一个空列表,则为 True,您真的希望条件为空列表还是变量为 None? -
@Chris Morgan:谢谢!我会仔细阅读并采用。我有点Java。 :-)
-
@Geekfish:我写了
if args.onlyTheseServers is not None:但是是的,如果 onlyTheseServers 是一个空列表,我确实希望条件为假。感谢您发现它!我将其更改为if args.onlyTheseServers: