473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Defining Multiple Objects at Once

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
4 6716
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter...
4
by: Andrew | last post by:
Hello, I am recieving a multiple definition error from the linker when I try to build a project I am working on. The message states that the functions I defined within an external .c source file...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
3
by: SL | last post by:
All, As I understand it, a single application (i.e. IIS virtual directory) in ASP.NET may in fact have more than one corresponding HttpApplicationState object (more or less one per server...
9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
7
by: Jay Douglas | last post by:
Greetings, I have a Windows form application that (naturally) instantiates all sorts of objects. I have a base object that contains an event. Lots of other objects inherit from this event. ...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
13
by: Eric IsWhoIAm | last post by:
I have four tables created so far: Courses, Instructors, Courses and Instructors (which shows the Course and Instructor Name fields, but holds their IDs since those are the keys), and Students....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.