Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use classes derived from Python's list class
    primarykey
    data
    text
    <p>This is a followup to question 912526 - <a href="https://stackoverflow.com/questions/912526/how-do-i-pass-lots-of-variables-to-and-from-a-function-in-python">How do I pass lots of variables to and from a function in Python?</a>.</p> <p>There are lots of variables that need to get passed around in the program I'm writing, and from my previous question I understand that I should put these variables into classes, and then pass around the classes.</p> <p>Some of these variables come in repetetive sets - for a thin film calculation I need to track the optical properties (index of refraction, absorption, thickness, etc) for a number of layers.</p> <p>Is the best way to store variables like this to create a class derived from a Python list to store the set of classes which each hold the variables for a single layer? And then put the functions that deal with the set of layers in class derived from the list, and the functions that deal with a specific layer in that class? Is there a better way to do this with a single class? </p> <p>Using the two class approach in the following example, I'm able to set things up so that I can access variables using statments like</p> <pre><code>n1 = layers[5].n </code></pre> <p>This is the best way to do this, right?</p> <pre><code>#Test passing values to and from functions class Layers(list): def add(self,n,k,comment): self.append( OneLayer(n,k,comment) ) def input_string(self): input_string = [] for layer in self: vars = layer.input_string() for var in vars: input_string.append( var ) return input_string def set_layers(self,results): for layer,i in enumerate(self): j = i*layer.num_var layer.set_layer( *results[j:j+2] ) class OneLayer(object): def __init__(self,n,k,comment): self.n = n self.k = k self.comment = comment def input_string(self): return [['f','Index of Refraction',self.n], ['f','Absorption',self.k],['s','Comment',self.comment]] def set_layer(self,n,k,comment): self.n = n; self.k=k; self.comment = comment def num_var(self): return 3 if __name__ == '__main__': layers = Layers() layers.add(1.0,0.0,'This vacuum sucks') layers.add(1.5,0.0,'BK 7 Glass') print layers[0].n print layers.input_string() layers[1].set_layer(1.77,0.0,'Sapphire') print layers.input_string() </code></pre> <p>I get the following output from this test program:</p> <pre><code>1.0 [['f', 'Index of Refraction', 1.0], ['f', 'Absorption', 0.0], ['s', 'Comment', 'This vacuum sucks'], ['f', 'Index of Refraction', 1.5], ['f', 'Absorption', 0.0], ['s', 'Comment', 'BK 7 Glass']] [['f', 'Index of Refraction', 1.0], ['f', 'Absorption', 0.0], ['s', 'Comment', 'This vacuum sucks'], ['f', 'Index of Refraction', 1.77], ['f', 'Absorption', 0.0], ['s', 'Comment', 'Sapphire']] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload