Python:初始化许多列表,良好的代码编写风格

标签 python initialization pep8

如果我在类的 __init__ 函数中将许多列表初始化为空,如下所示:

self.a = []
self.b = []
self.c = []
self.d = []
self.e = []

什么是更好的书写方式 - 每个列表单独或可能作为

self.a,self.b,self.c,self.d,self.e = []*5

或者也许还有其他好看的方法可以做到这一点?

最佳答案

a, b, c, d, e = ([] for _ in range(5))

关于Python:初始化许多列表,良好的代码编写风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38111970/

相关文章:

arrays - Swift:创建一个具有不同对象实例默认值的数组

swift - 错误: Variable with getter/setter cannot have an initial value

单变量初始化的 C++ 风格

python - 如何在 webpy 中获取当前请求的 url

python - C 程序可以有 Python GUI 吗?

python - 我应该在 Python 中为所有非 API 成员添加下划线前缀吗?

python - 为错误的 Python 版本引发错误的适当方法是什么?

python - 如何正确使用 python 的 isinstance() 检查变量是否为数字?

Python - OpenCV - 使用 HoughCircles 进行圆检测

python - Numpy 高级索引失败