Defining Multiple Objects at Once

S

SilverShadow

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.
 
H

Heather Coppersmith

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
 
L

Larry Bates

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.
 
S

SilverShadow

Thank you both for the input. I think that your suggestions will be very
helpful. I guess it is back to work then!

Greg
 
P

Peter Abel

SilverShadow said:
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.

Most of time one wants to put the instances in a definite namespace.
And in pyhton namespace handling is mostly organized by dictionaries.
So it's not necessary to declare an additional dictionary while the
namespace dictionary already exists.
And most of time the target namespace is the global namespace.

The following example will show, how to instantiate a bunch of
variables for the global namespace:
.... number_of_instances=0
.... def __init__(self):
.... Reactor.number_of_instances+=1
.... self.Reactor_Number=Reactor.number_of_instances
.... def show(self):
.... print 'Reactorname: Reactor#%d' % self.Reactor_Number
....
# A list of variablenames for Reactor-instances
# its a lazy declaration, cause I dont want to write commas
# and single quotmarks
reactor_name_list='R1 R2 R3 R4 R5'.split()
reactor_name_list ['R1', 'R2', 'R3', 'R4', 'R5']
# Add the instances with the declared names to the global
# namespace (it could be any else) which is represented by globals()
reduce(lambda last,name:globals().__setitem__(name,Reactor()),reactor_name_list,0)
# I took reduce rather than map, cause it doesnt create a list,
# which will be thrown away
# Remark the startvalue "0", cause otherwise "R1" wouldnt be instantiated
R1
R1.show() Reactorname: Reactor#1
R2.show() Reactorname: Reactor#2
# etc.
R5.number_of_instances 5
R5.show() Reactorname: Reactor#5

Hope I could help you.

Regards
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top