473,414 Members | 1,738 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,414 software developers and data experts.

Multiple instances of modules

Hi there.

I'm currently trying to learn Python, my aim being to create my final
year project at university using only Python instead of Java. I've run
into a problem though while trying to make multiple instances of
modules.

For example, say I made a CD.py module which held details of an audio
compact dic (album name, track names, etc..), and wanted my Python
script to make a record collection using multiple CD's, I can't make
them seperately. I tried using the 'import module as name' format, but
any changes made to one instance affects the other. I assume this is
because Python is simply referencing the one instance of CD.py rather
than seperate ones. Is there a solution to this that doesn't involve
using cPython or Jython? Any help would be greatly appreciated.
Jul 18 '05 #1
7 4438
Daniel Pryde wrote:
Hi there.

I'm currently trying to learn Python, my aim being to create my final
year project at university using only Python instead of Java. I've run
into a problem though while trying to make multiple instances of
modules.

For example, say I made a CD.py module which held details of an audio
compact dic (album name, track names, etc..), and wanted my Python
script to make a record collection using multiple CD's, I can't make
them seperately. I tried using the 'import module as name' format, but
any changes made to one instance affects the other. I assume this is
because Python is simply referencing the one instance of CD.py rather
than seperate ones. Is there a solution to this that doesn't involve
using cPython or Jython? Any help would be greatly appreciated.


I suppose that you misuse Python's module system. If you provide more
information about your project, more Pythonic solution could be found.

sincerely yours,
anton.

Jul 18 '05 #2

"Daniel Pryde" <ab********@hotmail.com> wrote in message
news:cf**************************@posting.google.c om...
Hi there.

I'm currently trying to learn Python, my aim being to create my final
year project at university using only Python instead of Java. I've run
into a problem though while trying to make multiple instances of
modules.

For example, say I made a CD.py module which held details of an audio
compact dic (album name, track names, etc..), and wanted my Python
script to make a record collection using multiple CD's, I can't make
them seperately. I tried using the 'import module as name' format, but
any changes made to one instance affects the other. I assume this is
because Python is simply referencing the one instance of CD.py rather
than seperate ones. Is there a solution to this that doesn't involve
using cPython or Jython? Any help would be greatly appreciated.


I suspect you're trying to store your data in the module itself.
That's not going to work because Python only loads one copy
of a module, by design.

As Anton suggests, please tell us a bit more about what you're
trying to do and the approach you're taking.

John Roth
Jul 18 '05 #3

Daniel> For example, say I made a CD.py module which held details of an
Daniel> audio compact dic (album name, track names, etc..), and wanted
Daniel> my Python script to make a record collection using multiple
Daniel> CD's, I can't make them seperately.

You don't want to use modules for this. You need to define a CD class
(perhaps within the CD module) and instantiate it for each album.

Skip

Jul 18 '05 #4
Daniel Pryde wrote:
Hi there.

I'm currently trying to learn Python, my aim being to create my final
year project at university using only Python instead of Java. I've run
into a problem though while trying to make multiple instances of
modules.

For example, say I made a CD.py module which held details of an audio
compact dic (album name, track names, etc..), and wanted my Python
script to make a record collection using multiple CD's, I can't make
them seperately. I tried using the 'import module as name' format, but
any changes made to one instance affects the other. I assume this is
because Python is simply referencing the one instance of CD.py rather
than seperate ones. Is there a solution to this that doesn't involve
using cPython or Jython? Any help would be greatly appreciated.


Use class instances to keep state, not modules. This is really the
basics of object-oriented programming and should be explained in the
(Python) tutorials.

-- Gerhard

Jul 18 '05 #5
On Mon, 15 Sep 2003, Daniel Pryde wrote:
For example, say I made a CD.py module which held details of an audio
compact dic (album name, track names, etc..), and wanted my Python
script to make a record collection using multiple CD's, I can't make
them seperately. I tried using the 'import module as name' format, but
any changes made to one instance affects the other. I assume this is
because Python is simply referencing the one instance of CD.py rather
than seperate ones. Is there a solution to this that doesn't involve
using cPython or Jython? Any help would be greatly appreciated.


Why not just use a class within the CD module? Then you could create as many
instances of that class as you'd like.

--
Paul Osman
pa**@eval.ca
http://perl.eval.ca

"Idealists...foolish enough to throw caution
to the winds...have advanced mankind and have
enriched the world."
- Emma Goldman

Jul 18 '05 #6

"Daniel Pryde" <ab********@hotmail.com>
I'm currently trying to learn Python, my aim being to create my final
year project at university using only Python instead of Java. I've run
into a problem though while trying to make multiple instances of
modules.


Can it be you want to instatiate a class? The class concept in Python has
nothing to do with moduls. You can define all your classes in your main
module. Read through some tutorials.

Kindly
Michael P
Jul 18 '05 #7
Daniel Pryde wrote:
Hi there.

I'm currently trying to learn Python, my aim being to create my final
year project at university using only Python instead of Java. I've run
into a problem though while trying to make multiple instances of
modules.

For example, say I made a CD.py module which held details of an audio
compact dic (album name, track names, etc..), and wanted my Python
script to make a record collection using multiple CD's, I can't make
them seperately. I tried using the 'import module as name' format, but
any changes made to one instance affects the other. I assume this is
because Python is simply referencing the one instance of CD.py rather
than seperate ones. Is there a solution to this that doesn't involve
using cPython or Jython? Any help would be greatly appreciated.


There is no connection between a class name and a module name enforced by
the language. If you already know a programming language, I strongly
recommend the tutorial that comes with the python distribution.

Below is a minimalist CD class with an example usage to get you started.
(In Python you say CD() instead of Java's new CD())

#module cd.py
class CD:
def __init__(self, composer, title):
self.composer = composer
self.title = title
def __str__(self):
return "%s: %s" % (self.composer, self.title)
#end module cd.py

#module main.py
from cd import CD
cdcoll = []
cdcoll.append(CD("Orff", "Carmina Burana"))
cdcoll.append(CD("Bach", "Goldberg-Variationen"))

for disk in cdcoll:
print disk
#end module main.py

Peter
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Miller | last post by:
Some background first - we have some software that embeds a Python interpreter into a host application. Scripts are loaded dynamically and used. But we want to support the ability to edit scripts...
1
by: Martin V. | last post by:
Hello, Is the multiple instances capability of SQL2K mature enough for a Production system? We're looking at upgrading our hardware but the proposed solution calls for consolidating two...
11
by: Mike | last post by:
Looking to find any information on how to properly configure multiple instances of DB2. This is on Win2k db2 ver 7.2. I am basically looking for information on how the multiple instance settings...
3
by: Ryan Muller | last post by:
I have a database that I designed for the production floor of my company. One of the problems that I have encountered is that 2 or more users will try to access the database to enter information...
4
by: Steve Jorgensen | last post by:
Hi all, This is actually not an issue of mine, but one a friend of mine told me about that I was able to confirm. Let's say you want a highly customizable report, so you want to be able to do...
9
by: jsale | last post by:
Hello, I am having a problem with multiple users using my asp.net application - namely that if each user clicks save at the same time, some, or all, of the users crash out. Do I need to do...
13
by: Robin Haswell | last post by:
Hey people I'm an experience PHP programmer who's been writing python for a couple of weeks now. I'm writing quite a large application which I've decided to break down in to lots of modules...
3
by: Marcin Kalicinski | last post by:
How do I use multiple Python interpreters within the same process? I know there's a function Py_NewInterpreter. However, how do I use functions like Py_RunString etc. with it? They don't take any...
0
by: bettatronic | last post by:
The goal : to have class which is able to add some OLE objects on the sheet runtime. Instances of the class must be accessible from any public/private module. Okay, I've experimented for so long &...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.