I have define a class in Python
class node(object):
def __init__(self, children = []):
self.children = children
I would like to create a list or array of default objects of class node
. For example, something like in C++
node * nodes = node[100];
then nodes
will point to an array of 100 default objects of class node
.
How is it done similarly in Python? Thanks.
Using a list comprehension:
nodes = [node() for _ in range(100)]
You can use a list comprehension:
nodes = [node() for _ in range(100)]
Python doesn't have a concept of "arrays" per se, but it has lists which are a higher-level structure. Lists can be indexed just like C arrays, and support many more complex operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With