How do I create dynamically, and manage, new instances of common data types in Python?
<p>I want to make a program which will ask the user for a number (let's say 3) and create three 3x3 lists, or 3 sets of 3 members or some other complex data type (times 3) Python already knows of. </p>

<p>Many programs create new objects without strict programming declaration of their instances. For example in Cinema4D (3D graphics software) i can push a button and create as many cubes I want. But I don't know the programming mechanics of this automatic instance creation without a written code declaration like:  </p>

<pre><code>cubeobj cube_1  
cube_1.name("Cube.1") ...
</code></pre>

<p>In C++ something like that would require the operator <code>new</code> and the function <code>malloc()</code>. Are there any equivalents for them in Python?</p>

<p>I've searched among many Python books and didn't find anything, what kind of Python topic would discuss something like that?</p>

<p>If you want a triple nested list, you can create it like this:</p>

<pre><code>In [1]: num = 3

In [2]: [[[[0 for __ in range(num)] for _ in range(num)]] for ___ in range(num)]
Out[2]: 
[[[[0, 0, 0], [0, 0, 0], [0, 0, 0]]],
 [[[0, 0, 0], [0, 0, 0], [0, 0, 0]]],
 [[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]]
</code></pre>

<p>Managing multidimensional arrays may be much easier with <code>numpy</code> though:</p>

<pre><code>In [3]: import numpy as np

In [4]: np.zeros((num, num, num))
Out[4]: 
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]]])
</code></pre>

<p>But the answer to your question in general is just that - you can create as many objects as you like by calling the corresponding constructor and keeping the returned instance accessible through a list, a dictionary or something else.</p>

<p>Using a hypothetical cube example, you can create <code>num</code> <code>Cube</code> objects and put them all in a list:</p>

<pre><code>num = int(input())
cubes = [Cube(name="Cube " + str(i)) for i in range(num)]
</code></pre>

python class object types