473,544 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.temperatur e(), 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 6720
On Wed, 26 May 2004 14:14:45 -0400,
"SilverShad ow" <GP*****@hotmai l.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.temperatur e(), 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_re actor( reactors[ 'R3' ] )
reactors[ 'R3' ].some_reactor_m ethod( )

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(Re actor())
rlist.append(Re actor())
rlist.append(Re actor())

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.

"SilverShad ow" <GP*****@hotmai l.com> wrote in message
news:46******** *************** *******@localho st.talkaboutpro gramming.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.temperatur e(), 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
"SilverShad ow" <GP*****@hotmai l.com> wrote in message news:<46******* *************** ********@localh ost.talkaboutpr ogramming.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.temperatur e(), 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_insta nces=0
.... def __init__(self):
.... Reactor.number_ of_instances+=1
.... self.Reactor_Nu mber=Reactor.nu mber_of_instanc es
.... def show(self):
.... print 'Reactorname: Reactor#%d' % self.Reactor_Nu mber
.... # A list of variablenames for Reactor-instances
# its a lazy declaration, cause I dont want to write commas
# and single quotmarks
reactor_name_li st='R1 R2 R3 R4 R5'.split()
reactor_name_li st ['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:globa ls().__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 <__main__.React or instance at 0x00DFC9D8> R1.show() Reactorname: Reactor#1 R2.show() Reactorname: Reactor#2 # etc.
R5.number_of_in stances 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
3185
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 to stdout/stderr, I'd like it to write to stdout, to a file, and/or call a logging function. So my output function might look something like this:...
4
30428
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 are already defined in the main program .obj file. I declared the functions in seperate header files and used the pre-processor conditional #ifndef...
32
14767
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
3
1915
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 thread, I think). During each request, only one of these objects is exposed to the page as Page.Application. This seems to be supported by the fact...
9
2760
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 wizards. But, I have found that trying to do something "outside the norm" adds a rather large level of complexity and/or data replication. Background I...
7
1807
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. When the base object or any derived object is instantiated I would like to automagically start listening for the event. An example and a further...
47
3598
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
52
6273
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 variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the...
13
3754
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. Now, I wish to create a Classrooms (or something similar) table which will allow me to pick the Course from Courses and Instructors, and hold multiple...
0
7414
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7359
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7360
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7699
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5895
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5288
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3398
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1833
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.