Defining Multiple Objects at Once

Heather Coppersmith me at privacy.net
Wed May 26 14:42:43 EDT 2004


On Wed, 26 May 2004 14:14:45 -0400,
"SilverShadow" <GPodubs at hotmail.com> wrote:

> I'm having trouble with something that may be easily remedied.
> I use Cantera running on Python.  I need to make multiple
> "Reactor()" objects and have them assigned a different (user
> defined) name.  For example:

> reactors = [R1, R2, R3...etc.]
> for reac in reactors:
>      reac = Reactor()

> My problem is there is no way to operate on each reactor
> separately.  (e.g.  R1.temperature()) The only thing that can be
> done is reac.temperature(), but that gets overwritten each time.
> So, my question is, is there any way to assign multiple names
> w/o having to write out lines of explicit definitions in the
> code?  Thank you in advance.

Put the objects into a dictionary, keyed by name:

    reactors = { }
    for reactor_name in 'R1', 'R2', 'R3', 'user defined name':
        reactors[ name ] = Reactor( )

and then, given a name, operate on them from there:

    operate_on_a_reactor( reactors[ 'R3' ] )
    reactors[ 'R3' ].some_reactor_method( )

HTH,
Heather

-- 
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli



More information about the Python-list mailing list