Module clarification

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hussein B

    Module clarification

    Hi.
    I'm a Java guy and I'm playing around Python these days...
    In Java, we organize our classes into packages and then jarring the
    packages into JAR files.
    What are modules in Python?
    What is the equivalent of modules in Java?
    Please correct me if I'm wrong:
    I saved my Python code under the file Wow.py
    Wow.py is now a module and I can use it in other Python code:
    import Wow

    Thanks.
  • Floris Bruynooghe

    #2
    Re: Module clarification

    On Jul 28, 9:54 am, Hussein B <hubaghd...@gma il.comwrote:
    Hi.
    I'm a Java guy and I'm playing around Python these days...
    In Java, we organize our classes into packages and then jarring the
    packages into JAR files.
    What are modules in Python?
    An importable or runable (i.e. script) collection of classes,
    functions, variables etc...
    What is the equivalent of modules in Java?
    Don't know. Not even sure if it exists, but my Java is old and never
    been great.
    Please correct me if I'm wrong:
    I saved my Python code under the file   Wow.py
    Wow.py is now a module and I can use it in other Python code:
    import Wow
    Indeed, you can now access things defined in Wow as Wow.foo


    Regards
    Floris

    Comment

    • Hussein B

      #3
      Re: Module clarification

      On Jul 28, 6:55 am, Floris Bruynooghe <floris.bruynoo ...@gmail.com>
      wrote:
      On Jul 28, 9:54 am, Hussein B <hubaghd...@gma il.comwrote:
      >
      Hi.
      I'm a Java guy and I'm playing around Python these days...
      In Java, we organize our classes into packages and then jarring the
      packages into JAR files.
      What are modules in Python?
      >
      An importable or runable (i.e. script) collection of classes,
      functions, variables etc...
      >
      What is the equivalent of modules in Java?
      >
      Don't know. Not even sure if it exists, but my Java is old and never
      been great.
      >
      Please correct me if I'm wrong:
      I saved my Python code under the file Wow.py
      Wow.py is now a module and I can use it in other Python code:
      import Wow
      >
      Indeed, you can now access things defined in Wow as Wow.foo
      >
      Regards
      Floris
      If I have a couple of modules, is there a way to package them? or
      there is no such a thing in Python?

      Comment

      • Diez B. Roggisch

        #4
        Re: Module clarification

        Hussein B wrote:
        Hi.
        I'm a Java guy and I'm playing around Python these days...
        In Java, we organize our classes into packages and then jarring the
        packages into JAR files.
        What are modules in Python?
        What is the equivalent of modules in Java?
        Read the docs:



        And read about eggs, the jars of python:



        Diez

        Comment

        • Duncan Booth

          #5
          Re: Module clarification

          Hussein B <hubaghdadi@gma il.comwrote:
          If I have a couple of modules, is there a way to package them? or
          there is no such a thing in Python?
          >
          >
          It sounds rather as though you haven't yet gone through the Python
          tutorial. You really should read it, even if you just skim through it to
          see what topics are covered. The tutorial explains both modules and
          packages: http://docs.python.org/tut/node8.html

          What it doesn't cover is that you can import modules or packages directly
          from a zip file.

          Then read about eggs.

          --
          Duncan Booth http://kupuguy.blogspot.com

          Comment

          • Brett Ritter

            #6
            Re: Module clarification

            On Jul 28, 4:54 am, Hussein B <hubaghd...@gma il.comwrote:
            Hi.
            I'm a Java guy and I'm playing around Python these days...
            In Java, we organize our classes into packages and then jarring the
            packages into JAR files.
            What are modules in Python?
            What is the equivalent of modules in Java?
            I'm new myself, coming from Perl and Java. Take my comments with the
            appropriate salt.

            Here's my understanding:

            1) JARs are a bit of a Java oddity. The other languages I've worked
            with don't really combine their packaging method for transport with
            their packaging method of access. Put another way, you may get a
            zipfile or tarball of library files, but they aren't USED in that
            format, they are just transported in that format. You unzip them and
            use the compiled libraries directly. Java appears to be unusual
            there. I could be wrong (it's a big world), but such is my experience
            in the C and Perl worlds.

            2) Java also dictates a single class per file (basically). Other
            languages do not have that restriction which leads to different
            collections. A file in Python (a module) may have several classes, or
            just one, or none. A package in Python is a directory containing
            modules (and possibly other packages) as well as a __init__.py file.
            This means that you cannot have the Java case of two packages offering
            the same fully qualified resource, because the namespace is tied to
            the filesystem (note you can alter this when importing the packages).

            3) Java uses "import" to create a shortcut to the namespace, a
            convenience for the programmer that has little to no bearing on the
            execution of the code. Namespace is determined by the classloader.
            Python uses "import" to declare how a namespace is used by the code
            itself, which can be very significant, (For example, Java can access
            any fully qualified package without an import statement. Python
            cannot access any package until it has been made available by import.)

            Hope that helps and is remotely accurate. I'm sure someone will
            correct me if I'm wrong.

            Comment

            • Hussein B

              #7
              Re: Module clarification

              On Jul 28, 8:11 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
              Hussein B <hubaghd...@gma il.comwrote:
              If I have a couple of modules, is there a way to package them? or
              there is no such a thing in Python?
              >
              It sounds rather as though you haven't yet gone through the Python
              tutorial. You really should read it, even if you just skim through it to
              see what topics are covered. The tutorial explains both modules and
              packages:http://docs.python.org/tut/node8.html
              >
              What it doesn't cover is that you can import modules or packages directly
              from a zip file.
              >
              Then read about eggs.
              >
              --
              Duncan Boothhttp://kupuguy.blogspo t.com
              I'm reading "Learning Python, 3rd Edition"
              What do you think about it?

              Comment

              • Marcus.CM

                #8
                Re: Module clarification

                Hi Hussein,

                Basically a module is a FILE and is considered as a singleton model. Yes
                ur wow.py assumption is correct.
                I recommend getting Mark Lutz Learning Python book to get you started.

                Marcus.CM

                Hussein B wrote:
                Hi.
                I'm a Java guy and I'm playing around Python these days...
                In Java, we organize our classes into packages and then jarring the
                packages into JAR files.
                What are modules in Python?
                What is the equivalent of modules in Java?
                Please correct me if I'm wrong:
                I saved my Python code under the file Wow.py
                Wow.py is now a module and I can use it in other Python code:
                import Wow
                >
                Thanks.
                --

                >
                >

                Comment

                Working...