What are modules and where do they come from?

Discussion in 'Scripting & Programming' started by tripwire45, Mar 19, 2008.

  1. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    180
    287
    The tutorial I'm currently working from to learn Python doesn't have a lot of "concept" information which is both good and bad. It's good in that it gets me to writing code fast so I can get my hands dirty and it's bad in that there isn't a lot of explanation to help me understand what things are and why they work.

    Chapter 12 is called "Using Modules" (and I finally found out why the "import something" statement is at the top of certain Python programs) but it doesn't really explain what a module is and where it comes from. The "import" implies that it comes from "outside" and into the program but where is its point of origin (I know I'm qualifying myself as a total moron here, but bear with me...no one is born knowing this stuff and I'm operating without the benefit of a degree in Computer Science)?

    I've tried looking online, but while I can find a great deal about specific Python modules, I can't find a simple definition. Same with the Python books I have access to. I know other programming languages (Perl, for one) use modules, but can anyone give me a simple, straightforward definition/explanation as to what a module is and where they are magically imported from?

    Thanks (once again, chagrined). :oops:
     
    Certifications: A+ and Network+
  2. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    This is just a stab in the dark, but it sounds similar to the imports/using features on vb.net/c#. Based on that, I would guess that its importing functions, etc into your script.

    In VB.Net, these are dll's (or other blocks of code - usually written by a third party, or by MS). The imports allows you to enter the namespace on the class, so that you can reference the functions.

    At its simplest, think about it like this. Say you have a couple of functions that you always use in your scripts. Rather than copying and pasting them into every single script you write, you could write them into a single script, and store it in a specific location. On each script you need it on, you would add the reference to that file, which would allow you to reference those functions. If you ever needed to change it, you would just need to modify one file.

    Its like the #include tags in html - you write a portion in a file thats common across a lot of files, and #include it in every file as required.

    Like i said, a stab in the dark, but i think im on the right lines.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  3. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    180
    287
    Certifications: A+ and Network+
  4. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Look at this :-

    http://www.python.org/doc/2.0.1/tut/node8.html

    The important thing to realise is that some programs can have millions of lines of code. Managing complexity and dependencies becomes a real issue. This is where namespaces, modules, libraries, classes and other forms of packaging and ecapsulation come in. They provide but also limit the access to pieces of code from other parts of the system. They make that bit of code usable throughout the system, while preserving code and state in a limited number of places. The DRY principle, (Don't Repeat Yourself), the seperation of concerns, create a good class or library that does one thing well, then leverage and let everone use it. Then you have to manage complexity, creating a layered architecture is one way of doing this. Theres other rules of thumb like avoiding cyclic dependencies between packages where possible.

    Theres a lot of rules of thumb, or design principles, at this stage it won't all make sense, just bear in mind creating 'components' is considered good practice as it simplifies the creation of complex systems be they hi-fis, cars or software.

    This is a well regarded book on the topic :-

    http://www.amazon.com/Large-Scale-Software-Addison-Wesley-Professional-Computing/dp/0201633620

    However unless you're into C++ and working as an architect on a large complex system its probably not for you.

    It is a bit dull and drawn out I have to admit that my copy is gathering dust and I never did finish it...
     
  5. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    180
    287
    Thanks for the link to the Modules tutorial. Don't worry about the link to the C++ book. Seems waaaaaaaaaay out of my league. :wink:
     
    Certifications: A+ and Network+
  6. tripwire45
    Honorary Member

    tripwire45 Zettabyte Poster

    13,493
    180
    287
    Certifications: A+ and Network+
  7. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    What I gave you to chew on was the overall code including the above. The above quoted code allows you to run your script independantly of specifically invoking python.

    Anyway, moving onto the importance and relevance of modules. With any programming language there is the notion of having a 'standard way' of performing particular tasks, and the standard way of doing anything is specific to the language in question. The developers of a language determine how tasks like printing to screen, arithmetic, etc. can be most efficiently implemented for the language in the most deterministic way. What do I mean by 'most efficient' and 'deterministic way'? The examples that we have already been working through should appropriately explain the former, in that you presented some code and we utilised the features of Python to squash it down taking fewer instructions to perform the same task. The latter involves ensuring that code specifically has expected results for any input that it expect to receive - that is the notion of 'determinism'. If code does not give one output for any single input, or its input otherwise is dependent on some level of probability, it is said to be 'non-deterministic'. Generally with code, unless the task specifically requires otherwise, all functionality should be deterministic.

    Going back to the importance of modules in a programming language. For the reasons described above all language come with standard modules that generally provide the most effecient and determinic tasks that the programmier is likely to need many times over. The programmer is also free to reinvent the wheel if they wish, but their implementation is likely to fall short of the standard provided by the included modules with the language.

    Another way to think about it is that Python itself had to be implemented using some other programming language (IIRC, there are C and Java implementations of the language), and these extra modules are merely extensions to the core language.

    I'm rambling, and the rest has been explained above. Am stopping now. :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.