Hello and welcome to CertForums.co.uk, here we host free active certification forums with links to the best free resources for Microsoft's MCSA MCSE MCDBA Cisco's CCNA CCDA and CCNP, and CompTIA's A+ Network+ i-NET+ and Security+ certifications in the UK. If you wish to post or use other advanced features you will need to register first. Registration is absolutely free and takes only a few minutes to complete so sign up today!
If you have any problems with the registration
process or your account login, please contact support
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).
You know, I wish my parents played Mozart when I slept because half the time I don't even know what the heck anyone's talking about!
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.
"Im Nerdy in the extreme and whiter than sour cream"
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.
As I recall from one of your posts in one of my threads, you said you were giving me something to "chew" on.
”
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.
Last edited by Mathematix : 20-Mar-2008 at 12:00 AM.