Defining Multiple Objects at Once

Larry Bates lbates at swamisoft.com
Wed May 26 14:50:38 EDT 2004


I don't know Cantera, but I think this
will help.

Something like the following works well:

rdict={'R1': None, 'R2': None, 'R3': None}
for reac in rdict.keys():
    #
    # Store an instance of Reactor class in
    # the dictionary with key reac
    #
    rdict[reac]=Reactor()

Then you can reference them with:

rdict['R1'].temperature()

You could also put them in a list instead
of a dictionary but then you would have to
reference them with an index:

rlist=[]
#
# Append an instance of Reactor class in
# the list.
#
rlist.append(Reactor())
rlist.append(Reactor())
rlist.append(Reactor())

then you can reference them with:

rlist[0].temperature()
rlist[1].temparature()

All depends on how you need to process them.

HTH,
Larry Bates
Syscon, Inc.

"SilverShadow" <GPodubs at hotmail.com> wrote in message
news:464afbb13c2bd25bb14351db772159e0 at localhost.talkaboutprogramming.com...
> Hello,
>
> 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.
>





More information about the Python-list mailing list