Automated Inclusion of Objects in Python List
<p>I'm declaring a class, and in the <code>__init__</code> method, I'm declaring two variables which will be associated with an instance of that class. Each declared variable is initialised as a NumPy array of zeros that is later filled with other data.</p>

<p>At some point, outside of this <code>__init__</code> method, I need to loop through an object containing each of these variables (NumPy arrays) and reset the array values in each to zero. Thus, in the <code>__init__</code> method, I would like to add all of these variables to a list to later iterate through. If I do that once manually, it's trivial.</p>

<p>However, I need to frequently add and remove some number of these variables being declared in <code>__init__</code>. Every time I do that, I don't want to have to manually adjust the variables that are contained in the list.</p>

<p>How can I create a <code>list</code>, <code>dict</code>, or other container of these variables that automatically adjusts itself so it contains all of these variables being initialised, regardless of the number of those variables? As an additional complication, <code>self.container_of_arrays</code> must not contain "other" variables from <code>__init__</code>, such as <code>self.array_length</code>.</p>

<p>What I have, where <code>self.container_of_arrays</code> must currently be manually adjusted if a <code>self.my_variable_three</code> is created:</p>

<pre><code>class generic_class():
    def __init__(self, array_width_one, array_width_two):
    self.array_length = some_integer

    self.my_variable_one = numpy.zeros((self.array_length, array_width_one), dtype=float)
    self.my_variable_two = numpy.zeros((self.array_length, array_width_two), dtype=float)

    self.container_of_arrays = [self.my_variable_one, self.my_variable_two]
</code></pre>

<p>I tried to declare my variables within a <code>dict</code>, so that from the very start they are contained within that <code>dict</code>, but this leads to there being references to undefined variables. E.g.</p>

<pre><code>    self.container_of_arrays = OrderedDict([(self.my_variable_one, numpy.zeros((self.array_length, array_width_one), dtype=float)),
                                            (self.my_variable_two, numpy.zeros((self.array_length, array_width_two), dtype=float))
                                           ])
</code></pre>

<p><strong>Update:</strong></p>

<p>xli's answer provides the correct solution. As suggested, it's possible to access the arrays by name using a dict or OrderedDict. For anyone who wants to do so, here's the code I've actually implemented:</p>

<pre><code>my_data = [('variable_one_name', len(variable_one_data)),
           ('variable_two_name', len(variable_two_data)),
          ] # Intentionally outside the class in a location where variables are added by the user

class generic_class():
    def __init__(self, array_length, my_data):

    self.container_of_arrays = OrderedDict((variable_name, numerix.zeros((array_length, array_width), dtype=float))
                                                    for variable_name, array_width in my_data)
</code></pre>

<p>Instead of creating a variable for each numpy array, why don't you just create a list of arrays containing each array you need, and reference them by index?</p>

<pre><code>class generic_class():
    def __init__(self, array_length, array_widths=[]):
        self.array_length = array_length
        self.arrays = [
             numpy.zeros((self.array_length, array_width), dtype=float) 
             for array_width in array_widths
        ]
</code></pre>

<p>Then you can access each array by index as <code>self.arrays[i]</code>.</p>

<p>Or, if you need to access the array by name, you can use a dict with just a string (the name of the array) as the key for each array. </p>

python list numpy dictionary containers