Python - call function of other object from attribute of a dynamically-created object
<p>Given a Windows ini-style configuration file, e.g. 'airplanes.ini':</p>

<pre><code>[JumboJet]
wingspan = 211
length = 231
seating = 416
crew = 2
unit_cost = 234000000
on_hand = 3

[SopwithCamel]
wingspan = 28
length = 18
armament = twin Vickers
crew = 1
on_hand = 1

[NCC1701]
length = 289 meters
crew = 430
speed = Warp 8
armament = 12 phasers, 6 photon torpedo
</code></pre>

<p>I use the ConfigParser module from the Python 2.7.3 library to read in the contents of the file, then use the built-in <code>type()</code> function to create a new object of type "Airplane" for each [section] in the config file. Each <code>name = value</code> pair becomes an attribute of the object:</p>

<pre class="lang-python prettyprint-override"><code># create a config parser, using SafeConfigParser for variable substitution
config = ConfigParser.SafeConfigParser()

# read in the config file
config.read('airplanes.ini')

airplanes = []

# loop through each "[section]" of config file
for section in config.sections():
    # create a new object of type Airplane
    plane = type("Airplane",(object,),{"name":section})

    # loop through name = value pairs in section
    for name, value in config.items(section)
        # this is where the magic happens?
        setattr(plane, name, lambda: config.set(section,name,value))

    airplanes.append(plane)

# do stuff with Airplanes,
boeing = airplanes[1]

# this update needs to call through to config.set()
boeing.on_hand = 2

# then save the changes out to the config file on disk
with open('airplanes.ini','wb') as f:
    config.write(f)
</code></pre>

<p>The line commented "this is where the magic happens" indicates where I want to set the call to ConfigParser's <code>set()</code> method, through the attribute's "setter", to update the config object. I believe <code>setattr(plane, name, value)</code> is the "usual" way to create the attribute, but that won't call <code>config.set()</code>.</p>

<p>I want the flexibility to dynamically define the attributes of the objects as the items in  each section of the config file, even if the items in each section differ, or each section has different numbers of items. </p>

<p>Any suggestions on how I might implement this? I don't think property() or setattr() will do what I want.</p>
