Multiple instances of modules

D

Daniel Pryde

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.
 
A

anton muhin

Daniel said:
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.
 
S

Skip Montanaro

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
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

Daniel said:
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
 
P

Paul Osman

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
(e-mail address removed)
http://perl.eval.ca

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

John Roth

Daniel Pryde said:
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
 
M

Michael Peuser

Daniel Pryde said:
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
 
P

Peter Otten

Daniel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top