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

Multiple instances of modules

P: n/a
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
Share this Question
Share on Google+
7 Replies


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

P: n/a

"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

P: n/a

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

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

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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.