Multiple instances of modules

Discussion in 'Python' started by Daniel Pryde, Sep 15, 2003.

  1. Daniel Pryde

    Daniel Pryde Guest

    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.
     
    Daniel Pryde, Sep 15, 2003
    #1
    1. Advertising

  2. Daniel Pryde

    anton muhin Guest

    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.
     
    anton muhin, Sep 15, 2003
    #2
    1. Advertising

  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
     
    Skip Montanaro, Sep 15, 2003
    #3
  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
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Sep 15, 2003
    #4
  5. Daniel Pryde

    Paul Osman Guest

    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

    http://perl.eval.ca

    "Idealists...foolish enough to throw caution
    to the winds...have advanced mankind and have
    enriched the world."
    - Emma Goldman
     
    Paul Osman, Sep 15, 2003
    #5
  6. Daniel Pryde

    John Roth Guest

    "Daniel Pryde" <> wrote in message
    news:...
    > 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
     
    John Roth, Sep 15, 2003
    #6
  7. "Daniel Pryde" <>

    > 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
     
    Michael Peuser, Sep 15, 2003
    #7
  8. Daniel Pryde

    Peter Otten Guest

    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
     
    Peter Otten, Sep 15, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joerg Schrocke

    Multiple instances of web application

    Joerg Schrocke, Oct 21, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    2,501
    manger22
    Nov 22, 2005
  2. hoeeg (at) post4.tele.dk

    Problem with multiple instances of a module

    hoeeg (at) post4.tele.dk, Apr 20, 2005, in forum: Perl
    Replies:
    1
    Views:
    2,574
    Alan D. Salewski
    Apr 22, 2005
  3. John Wohlbier
    Replies:
    2
    Views:
    398
    Josiah Carlson
    Feb 22, 2004
  4. Replies:
    8
    Views:
    488
    James Stroud
    Jan 29, 2009
  5. Nav
    Replies:
    15
    Views:
    577
    Steven D'Aprano
    Jan 5, 2010
Loading...

Share This Page