python - 在 Python 中创建一个类的默认对象列表?

标签 python

我在Python中定义了一个类

class node(object):
    def __init__(self, children = []):
        self.children = children

我想创建类 node 的默认对象列表或数组。例如,像 C++ 中的东西

node * nodes = node[100];

然后 nodes 将指向一个包含 100 个默认对象的数组 node

它是如何在 Python 中完成的?谢谢。

最佳答案

使用列表理解:

nodes = [node() for _ in range(100)]

关于python - 在 Python 中创建一个类的默认对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25436874/

相关文章:

python - 按降序对相应字符串的数字进行排序

python - 在python中进行大计算时出现段错误

Java 通过引用转码到 Python

python - 关于情绪分析的问题

python - TCP 客户端-服务器代码。什么都不做

python - networkx 中的 nx.get_node_attributes 返回一个空字典

python - 带有日期时间的 Numba

python - 如何按顺序标记 Pandas 中的分类变量?

python - pyspark Py4J 错误使用 canopy :PythonAccumulatorV2([class java. lang.String, class java.lang.Integer, class java.lang.String]) does not exist

python - 从 .py 文件加载的 Monkeypatching 硬编码全局配置