Python Packages, Modules and Classes
Python says Don't Repeat Yourself. This is made possible by module. A module is just a python script that is 'included' into another one. The syntax for doing that though is with the import keyword. For example if you have a file named crazyModule.py with some code in it you can import it into another script with
import crazyModule. You can then access functions in it as
crazyModule.func2() and so on and so forth. Or if you are too lazy to keep typing the module name repeatedly you can try:
from crazyModule import func1, func2
That eliminates the need to prefix everything with the module name. All scripts have access to a predefined variable called
__name__ which as you can guess gives the name of the script. When you call it from the main script it will be set to
__main__ and inside a module it will have the name of the module. This feature is usefull if you write a bit of code that can be executed on it's own right or can be used as a module.
'The built-in function
dir() is used to find out which names a module defines. It returns a sorted list of strings:' (python tutorial). Here a name means a variable or a function. "
dir() does not list the names of built-in functions and variables. If you want a list of those, they are defined in the standard module
Too many verbatim quotes from the tutorial already but please do allow me another : "Packages are a way of structuring Python’s module namespace by using 'dotted module names'" this is similar at least in notation and file structure to java packages, with each dot actually representing a subfolder in the
sys.path. However a module need not be a class like in Java or Ruby (in Ruby module actually means something else but the import mechanism exists by another name). The other difference is that each folder needs to have a __init__.py . The file can be empty but it has to be there.
Here is the simplest class that you can make with Python
class myclass: pass
It's not compulsory to inherit from some super class but if you do need to make derived class the format looks like:
class myclass(SuperClass): pass
It sure looks like a function call doesn't it? Python classes have a method named
__init__ that looks like a constructor, smells like one and even sounds like a constructor (Dive into python) but the damned thing is not a constructor. It's actually called after the object has been made. Worse still, current instance is passed as the first parameter! This gets weirder; all class methods need to have
self defined as the first parameter, but you don't need to pass it in as a parameter when calling those methods because python will add it automatically! (Dive into python)
Defining an __init__ method is option but if you do make one, you must call the ancestors __init__ method excplicitly, and when you do so you need to explicitly add self as a parameter.
We have already discussed about using triple quoted strings as the first line in function to serve as it's documentation (we haven't? well you know now). The same thing is possible with classes. If you create such a 'doc string' , it can be retrieved with the
How do you create a new instance of a class? certainly not with new! you just pretend the class is a function.
myObjet = MyClass(params)
Instance variables (in python these are called data attributes) don't need to be declared. They come into existence when they are first assigned (PHP like behaviour). But then looking at the following statement in the python tutorial, one wonders why we even bother with classes in python
Please allow me another quote: "Data attributes override method attributes with the same name; to avoid accidental name conflicts, which may cause hard-to-find bugs in large programs, it is wise to use some kind of convention that minimizes the chance of conflicts."
Attributes may be referenced by methods as well as by ordinary users (“clients”) of an object. In other words, classes are not usable to implement pure abstract data types. In fact, nothing in Python makes it possible to enforce data hiding
Python is language that does not pay lip service to the multiple inheritance is evil mantra. You can make a class inherit from many like so:
myClass(super1, super2 ... , supern)
Objects can be serialized and deserliazed with Pickle.
import pickle pickle.dump([1,2,3,'aaaa',5],file_handle)
Easy huh? How do you get out of the pickle you are in? with pickle.load(file_handle). Use sys.stdout and sys.stdin if you just want to see how this works.As a footnote, I decided to try out python in 2008 but that project came to note, but at the time I ran into an error named 'ImportError: No module named dom.minidom' and my post on it has had a surprising number of views.