By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

Defining Multiple Objects at Once

P: n/a
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.

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Wed, 26 May 2004 14:14:45 -0400,
"SilverShadow" <GP*****@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
Jul 18 '05 #2

P: n/a
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" <GP*****@hotmail.com> wrote in message
news:46******************************@localhost.ta lkaboutprogramming.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.

Jul 18 '05 #3

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

Greg

Jul 18 '05 #4

P: n/a
"SilverShadow" <GP*****@hotmail.com> wrote in message news:<46******************************@localhost.t alkaboutprogramming.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.


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:
# An example Reactor class, to show different instances
class Reactor: .... 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()),re actor_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 <__main__.Reactor instance at 0x00DFC9D8> 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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.