Δημοσιεύτηκε: 28 Μάιος 2009, 10:08
από dimosfire
Kαλημέρα παιδιά, υποβάλλω το κεφάλαιο 10
Κώδικας: Επιλογή όλων
Python en:Modules
Introduction
You have seen how you can reuse code in your program by defining functions once. What if
you wanted to reuse a number of functions in other programs that you write? As you might
have guessed, the answer is modules.
There are various methods of writing modules, but the simplest way is to create a file with a
.py extension that contains functions and variables.
Another method is to write the modules in the native language in which the Python
interpreter itself was written. For example, you can write modules in the C programming
language (http:/ / docs. python. org/ extending/ ) and when compiled, they can be used from
your Python code when using the standard Python interpreter.
A module can be imported by another program to make use of its functionality. This is how
we can use the Python standard library as well. First, we will see how to use the standard
library modules.
Example:
#!/usr/bin/python
# Filename: using_sys.py
import sys
print('The command line arguments are:')
for i in sys.argv:
print(i)
print('\n\nThe PYTHONPATH is', sys.path, '\n')
Output:
$ python using_sys.py we are arguments
The command line arguments are:
using_sys.py
we
are
arguments
The PYTHONPATH is ['', 'C:\\Windows\\system32\\python30.zip',
'C:\\Python30\\DLLs', 'C:\\Python30\\lib',
'C:\\Python30\\lib\\plat-win', 'C:\\Python30',
'C:\\Python30\\lib\\site-packages']
How It Works:
First, we import the sys module using the import statement. Basically, this translates to
us telling Python that we want to use this module. The sys module contains functionality
related to the Python interpreter and its environment i.e. the system.
Python en:Modules 56
When Python executes the import sys statement, it looks for the sys module. In this case,
it is one of the built-in modules, and hence Python knows where to find it.
If it was not a compiled module i.e. a module written in Python, then the Python interpreter
will search for it in the directories listed in its sys.path variable. If the module is found,
then the statements in the body of that module is run and then the module is made
available for you to use. Note that the initialization is done only the first time that we
import a module.
The argv variable in the sys module is accessed using the dotted notation i.e. sys.argv. It
clearly indicates that this name is part of the sys module. Another advantage of this
approach is that the name does not clash with any argv variable used in your program.
The sys.argv variable is a list of strings (lists are explained in detail in a later chapter.
Specifically, the sys.argv contains the list of command line arguments i.e. the arguments
passed to your program using the command line.
If you are using an IDE to write and run these programs, look for a way to specify command
line arguments to the program in the menus.
Here, when we execute python using_sys.py we are arguments, we run the module
using_sys.py with the python command and the other things that follow are arguments
passed to the program. Python stores the command line arguments in the sys.argv
variable for us to use.
Remember, the name of the script running is always the first argument in the sys.argv
list. So, in this case we will have 'using_sys.py' as sys.argv[0], 'we' as sys.argv[1],
'are' as sys.argv[2] and 'arguments' as sys.argv[3]. Notice that Python starts
counting from 0 and not 1.
The sys.path contains the list of directory names where modules are imported from.
Observe that the first string in sys.path is empty - this empty string indicates that the
current directory is also part of the sys.path which is same as the PYTHONPATH
environment variable. This means that you can directly import modules located in the
current directory. Otherwise, you will have to place your module in one of the directories
listed in sys.path.
Note that the current directory is the directory from which the program is launched. Run
import os; print(os.getcwd()) to find out the current directory of your program.
Byte- compiled . pyc files
Importing a module is a relatively costly affair, so Python does some tricks to make it faster.
One way is to create byte-compiled files with the extension .pyc which is an intermediate
form that Python transforms the program into (remember the introduction section on how
Python works?). This .pyc file is useful when you import the module the next time from a
different program - it will be much faster since a portion of the processing required in
importing a module is already done. Also, these byte-compiled files are
platform-independent.
Note
These .pyc files are usually created in the same directory as the corresponding .py
files. If Python does not have permission to write to files in that directory, then the
.pyc files will not be created.
Python en:Modules 57
The from . . . import . . . statement
If you want to directly import the argv variable into your program (to avoid typing the
sys. everytime for it), then you can use the from sys import argv statement. If you want
to import all the names used in the sys module, then you can use the from sys import *
statement. This works for any module.
In general, you should avoid using this statement and use the import statement instead
since your program will avoid name clashes and will be more readable.
A module's _ _ name_ _
Every module has a name and statements in a module can find out the name of its module.
This is handy in the particular situation of figuring out if the module is being run standalone
or being imported. As mentioned previously, when a module is imported for the first time,
the code in that module is executed. We can use this concept to alter the behavior of the
module if the program was used by itself and not when it was imported from another
module. This can be achieved using the __name__ attribute of the module.
Example:
#!/usr/bin/python
# Filename: using_name.py
if __name__ == '__main__':
print('This program is being run by itself')
else:
print('I am being imported from another module')
Output:
$ python using_name.py
This program is being run by itself
$ python
>>> import using_name
I am being imported from another module
>>>
How It Works:
Every Python module has it's __name__ defined and if this is '__main__', it implies that the
module is being run standalone by the user and we can take appropriate actions.
Python en:Modules 58
Making Your Own Modules
Creating your own modules is easy, you've been doing it all along! This is because every
Python program is also a module. You just have to make sure it has a .py extension. The
following example should make it clear.
Example:
#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
print('Hi, this is mymodule speaking.')
__version__ = '0.1'
# End of mymodule.py
The above was a sample module. As you can see, there is nothing particularly special about
compared to our usual Python program. We will next see how to use this module in our
other Python programs.
Remember that the module should be placed in the same directory as the program that we
import it in, or the module should be in one of the directories listed in sys.path.
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print ('Version', mymodule.__version__)
Output:
$ python mymodule_demo.py
Hi, this is mymodule speaking.
Version 0.1
How It Works:
Notice that we use the same dotted notation to access members of the module. Python
makes good reuse of the same notation to give the distinctive 'Pythonic' feel to it so that we
don't have to keep learning new ways to do things.
Here is a version utilising the from..import syntax:
#!/usr/bin/python
# Filename: mymodule_demo2.py
from mymodule import sayhi, __version__
sayhi()
print('Version', __version__)
Python en:Modules 59
The output of mymodule_demo2.py is same as the output of mymodule_demo.py.
Notice that if there was already a __version__ name declared in the module that imports
mymodule, there would be a clash. This is also likely because it is common practice for each
module to declare it's version number using this name. Hence, it is always recommended to
prefer the import statement even though it might make your program a little longer.
You could also use:
from mymodule import *
This will import all public names such as sayhi but would not import __version__
because it starts with double underscores.
Zen of Python
One of Python's guiding principles is that "Explicit is better than Implicit". Run import
this to learn more and see this discussion (http:/ / stackoverflow. com/ questions/
228181/ zen-of-python) which lists examples for each of the principles.
The dir function
You can use the built-in dir function to list the identifiers that an object defines. For
example, for a module, the identifiers include the functions, classes and variables defined in
that module.
When you supply a module name to the dir() function, it returns the list of the names
defined in that module. When no argument is applied to it, it returns the list of names
defined in the current module.
Example:
$ python
>>> import sys # get list of attributes, in this case, for the sys module
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__',
'__package__', '__s
tderr__', '__stdin__', '__stdout__', '_clear_type_cache',
'_compact_freelists',
'_current_frames', '_getframe', 'api_version', 'argv',
'builtin_module_names', '
byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook',
'dllhandle'
, 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
'executable',
'exit', 'flags', 'float_info', 'getcheckinterval',
'getdefaultencoding', 'getfil
esystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount',
'getsizeof',
'gettrace', 'getwindowsversion', 'hexversion', 'intern', 'maxsize',
'maxunicode
', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache',
Python en:Modules 60
'platfor
m', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setprofile',
'setrecursionlimit
', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version',
'version_in
fo', 'warnoptions', 'winver']
>>> dir() # get list of attributes for current module
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>> a = 5 # create a new variable 'a'
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'sys']
>>> del a # delete/remove a name
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>>
How It Works:
First, we see the usage of dir on the imported sys module. We can see the huge list of
attributes that it contains.
Next, we use the dir function without passing parameters to it. By default, it returns the
list of attributes for the current module. Notice that the list of imported modules is also part
of this list.
In order to observe the dir in action, we define a new variable a and assign it a value and
then check dir and we observe that there is an additional value in the list of the same
name. We remove the variable/attribute of the current module using the del statement and
the change is reflected again in the output of the dir function.
A note on del - this statement is used to delete a variable/name and after the statement
has run, in this case del a, you can no longer access the variable a - it is as if it never
existed before at all.
Note that the dir() function works on any object. For example, run dir(print) to learn
about the attributes of the print function, or dir(str) for the attributes of the str class.
Python en:Modules 61
Packages
By now, you must have started observing the hierarchy of organizing your programs.
Variables usually go inside functions. Functions and global variables usually go inside
modules. What if you wanted to organize modules? That's where packages come into the
picture.
Packages are just folders of modules with a special __init__.py file that indicates to
Python that this folder is special because it contains Python modules.
Let's say you want to create a package called 'world' with subpackages 'asia', 'africa', etc.
and these subpackages in turn contain modules like 'india', 'madagascar', etc.
This is how you would structure the folders:
- <some folder present in the sys.path>/
- world/
- __init__.py
- asia/
- __init__.py
- india/
- __init__.py
- foo.py
- africa/
- __init__.py
- madagascar/
- __init__.py
- bar.py
Packages are just a convenience to hierarchically organize modules. You will see many
instances of this in the standard library.
Summary
Just like functions are reusable parts of programs, modules are reusable programs.
Packages are another hierarchy to organize modules. The standard library that comes with
Python is an example of such a set of packages and modules.
We have seen how to use these modules and create our own modules.
Next, we will learn about some interesting concepts called data structures.
Previous Next
Source: http:/ / www. swaroopch. com/ mediawiki/ index. php? oldid=2371
Contributors: Swaroop, 7 anonymous edits

και τη μετάφραση:
Κώδικας: Επιλογή όλων
ΑΡΘΡΩΜΑΤΑ(MODULES)


Εισαγωγή

Εχετε δει πως μπορείτε να επαναχρησιμοποιήσετε κώδικα στο πρόγραμμά σας ορίζοντας μια φορά συναρτήσεις. Τι κάνετε όμως αν θέλετε να επαναχρησιμοποιήσετε έναν αριθμό συναρτήσεων σε άλλα προγράμματα που γράφετε; Oπως ίσως έχετε μαντέψει η απάντηση είναι τα αρθρώματα.
Υπάρχουν διάφοροι μεθόδοι γαι να γράφετε αρθρώματα, αλλά ο απλούστερος τρόπος είναι δημιουργώντας ένα αρχείο με επέκταση .py το οποίο θα περιέχει συναρτήσεις και μεταβλητές.
Ενας άλλος τρόπος είναι να γράφετε τα αρθρώματα στην πρώτη (γεννησιουργό) γλώσσα στην οποία γράφτηκε o διερμηνευτής της Python. Για παράδειγμα μπορείτε να γράψετε αρθρώματα στην γλώσσα προγραμματισμού C (http://docs.python.org/extending/) και μόλις μεταγλωτιστούν, να χρησιμοποιηθούν από τον κώδικα που γράφετε σε Python όταν χρησιμοποιείτε τον πρότυπο διερμηνευτή της Python.
Ενα άρθρωμα μπορεί να εισαχθεί από ένα άλλο πρόγραμμα για να κάνετε χρήση της λειτουργικότητάς του.
Ετσι μπορείτε να χρησιμοποιήσετε επίσης την πρότυπη βιβλιοθήκη της Python. Aρχικά θα δείτε πως να χρησιμοποιείτε τις πρότυπες βιβλιοθήκες αρθρωμάτων.

Παράδειγμα:
#!/usr/bin/python
# Filename: using_sys.py
import sys
print('The command line arguments are:')
for i in sys.argv:
print(i)
print('\n\nThe PYTHONPATH is', sys.path, '\n')

Αποτέλεσμα:
$ python using_sys.py we are arguments
The command line arguments are:
using_sys.py
we
are
arguments
The PYTHONPATH is ['', 'C:\\Windows\\system32\\python30.zip',
'C:\\Python30\\DLLs', 'C:\\Python30\\lib',
'C:\\Python30\\lib\\plat-win', 'C:\\Python30',
'C:\\Python30\\lib\\site-packages']

Πως λειτουργεί:
Aρχικά εισάγετε το άρθρωμα sys χρησιμοποιώντας την εντολή import. Κατά κύριο λόγο αυτό μεταφράζει σε εσάς λέγοντας στην Python ότι θέλουμε να χρησιμοποιήσουμε αυτό το άρθρωμα. Το άρθρωμα sys περιέχει λειτουργικότητα που έχει σχέση με τον διερμηνευτή της Python και το περιβάλλον του δηλ. το sys(tem).
Oταν η Python εκτελεί την εντολή import sys, ψάχνει για το άρθρωμα sys. Σε αυτή την περίπτωση, είναι ένα από τα ενσωματωμένα αρθρώματα, και γι αυτό η Python ξέρει που θα το βρει.
Εάν δεν ήταν ένα ενσωματωμένο άρθρωμα δηλ. ένα άρθρωμα γραμμένο σε Python, τότε ο διερμηνευτής της Python θα το έψαχνε στους καταλόγους που βρίσκονται στη μεταβλητή του sys.path. Εάν το άρθρωμα βρεθεί τότε οι εντολές στο κυρίως τμήμα του αρθρώματος τρέχουν και τότε το άρθρωμα σας γίνεται διαθέσιμο για να το χρησιμοποιήσετε. Σημειώστε ότι η αρχικοθέτηση γίνεται μόνο την πρώτη φορά που εισάγουμε ένα άρθρωμα.
Η μεταβλητή sys.argv στο άρθρωμα sys εισάγεται χρησιμοποιώντας συμβολισμό με τελείες δηλ. sys.argv. Αυτό δείχνει ξεκάθαρα ότι αυτή η ονομασία είναι μέρος του αρθρώματος sys. Ενα ακόμα πλεονέκτημα αυτής της προσέγγισης είναι ότι αυτή η ονομασία δεν συγκρούεται με καμμία άλλη argv μεταβλητή που χρησιμοποιείτε στο πρόγραμμά σας. Η μεταβλητή sys.argv είναι ένας κατάλογος (list) συμβολοσειρών (strings). Eιδικά η sys.argv περιέχει τον κατάλογο των ορισμάτων της γραμμής εντολών (command line arguments) δηλ. τα ορίσματα που έχουν περάσει στο σύστημά σας χρησιμοποιώντας την γραμμή εντολών. Εάν χρησιμοποιείς IDE (Integrated Development Enviroment δηλ. ολοκληρωμένο περιβάλλον ανάπτυξης) για να γράψεις και να τρέξεις αυτά τα προγράμματα, ψάξε έναν τρόπο να καθορίζεις ορίσματα γραμμής εντολών στο πρόγραμμα στους καταλόγους επιλογών.
Εδώ όταν εκτελούμε python using_sys.py we are arguments, τρέχουμε το άρθρωμα using_sys.py με την εντολή python και τα άλλα στοιχεία που ακολουθούν είναι ορίσματα περασμένα στο πρόγραμμα.Η Python αποθηκεύει τα ορίσματα της γραμμής εντολών στη μεταβλητή sys.argv για να τα χρησιμοποιήσετε.

Θυμηθείτε ότι η ονομασία του σεναρίου που τρέχει είναι πάντα το πρώτο όρισμα στον κατάλογο sys.argv. Eτσι σε αυτή την περίπτωση θα έχουμε το 'using_sys.py' σαν 'sys.argv[0], το 'we' σαν sys.argv[1], το 'are' σαν sys.argv[2] και το 'arguments' σαν sys.argv[3]. Σημειώστε ότι η Python αρχίζει να μετράει από το 0 και όχι από το 1.
Το sys.path περιέχει τη λίστα με τα ονόματα καταλόγων από όπου εισάγονται τα αρθρώματα. Παρατηρήστε ότι η πρώτη συμβολοσειρά στο sys.path είναι άδεια. Αυτή η άδεια συμβολοσειρά δείχνει ότι ο τρέχων κατάλογος είναι επίσης μέρος του sys.path, o οποίος είναι το ίδιο με τη μεταβλητή περιβάλλοντος PYTHONPATH. Αυτό σημαίνει ότι μπορείς απευθείας να εισάγεις αρθρώματα που βρίσκονται στον τρέχοντα κατάλογο. Διαφορετικά πρέπει να τοποθετήσεις το άρθρωμά σου σε έναν από τους καταλόγους που βρίσκονται στο sys.path. Σημειώστε ότι ο τρέχων κατάλογος είναι ο κατάλογος από όπου το πρόγραμμα εκκινείται. Τρέξτε import (os; print(os.getcwd() ) για να βρείτε τον τρέχοντα κατάλογο του προγράμματός σας.

Μεταγλώττιση-ΒΥΤΕ σε αρχεία με επέκταση .pyc
Η εισαγωγή ενός αρθρώματος είναι μια υπόθεση που κοστίζει, έτσι η Python κάνει μερικά κόλπα για να το κάνετε γρηγορότερα. Ενας τρόπος είναι να δημιουργήσετε αρχεία μεταγλώττισης byte (byte-compiled files) με την επέκταση .pyc η οποία είναι μια ενδιάμεση φόρμα που η Python μετατρέπει το πρόγραμμα (θυμηθείτε το εισαγωγικό τμήμα του βιβλίου πως λειτουργεί η Python). Αυτό το αρχείο με επέκταση .pyc είναι χρήσιμο την επόμενη φορά που θα εισάγετε το άρθρωμα από ένα διαφορετικό πρόγραμμα. Αυτό θα είναι πολύ ταχύτερο από το να απαιτείται ένα τμήμα της διεργασίας κατά την εισαγωγή ενός αρθρώματος που έχει ήδη γίνει. Επίσης τα byte-compiled αρχεία είναι ανεξάρτητα πλατφόρμας (platform-independent).
Σημείωση:Τα αρχεία .pyc συνήθως δημιουργούνται στον ίδιο κατάλογο με τα αντίστοιχα αρχεία .py. Εάν η Python δεν έχει άδεια για να γράψει σε αρχεία σε αυτόν τον κατάλογο, τότε τα αρχεία .pyc δεν θα δημιουργηθούν.

Η εντολή from...import...
Eάν θέλετε να εισάγετε απευθείας τη μεταβλητή argv μέσα στο πρόγραμμά σας (για να αποφύγετε την πληκτρολόγηση του sys. κάθε φορά), τότε μπορείτε να χρησιμοποιήσετε την εντολή from sys import argv. Εάν θέλετε να εισάγετε όλες τις ονομασίες που χρησιμοποιούνται στο άρθρωμα sys, τότε μπορείτε να χρησιμοποιήσετε την εντολή from sys import *. Αυτό λειτουργεί σε κάθε άρθρωμα. Γενικά πρέπει να αποφεύγετε να χρησιμοποιείτε αυτή την εντολή και αντ' αυτής να χρησιμοποιείτε την εντολή import επειδή έτσι το πρόγραμμά σας θα αποφύγει την σύγκρουση των ονομασιών και θα είναι πιο ευανάγνωστο.

Ονομασία αρθρώματος (module's__name__)
Κάθε άρθρωμα έχει μια ονομασία και οι εντολές σε ένα άρθρωμα μπορούν να ανακαλύψουν το όνομα του αρθρώματός τους. Αυτό είναι εύχρηστο στην ειδική κατάσταση του υπολογισμού για το εάν το άρθρωμα τρέχει μόνο του ή εισάγεται. Οπως αναφέρθηκε προηγουμένως, όταν ένα άρθρωμα εισάγεται για πρώτη φορά, ο κώδικας σε αυτό το άρθρωμα εκτελείται. Μπορούμε να χρησιμοποιήσουμε αυτή την έννοια για να αλλάξουμε την συμπεριφορά του αρθρώματος εάν το πρόγραμμα χρησιμοποιείται από μόνο του και όχι όταν εισάγεται από ένα άλλο άρθρωμα. Αυτό μπορεί να επιτευχθεί χρησιμοποιώντας το ιδιοχαρακτηριστικό __name__ του αρθρώματος.

Παράδειγμα:
#!/usr/bin/python
# Filename: using_name.py
if __name__ == '__κύριο__':
print('Aυτό το πρόγραμμα τρέχει από μόνο του')
else:
print('Εχω εισαχθεί από ένα άλλο άρθρωμα')

Αποτέλεσμα:
$ python using_name.py
Αυτό το πρόγραμμα τρέχει από μόνο του
$ python
>>> import using_name
Εχω εισαχθεί από ένα άλλο άρθρωμα
>>>

Πως λειτουργεί:
Σε κάθε άρθρωμα στην Python έχει ορισθεί η ονομασία του' __name__ ' και εάν αυτή είναι κύρια '__main__, τότε συνεπάγεται ότι τρέχει μόνο του από το χρήστη και μπορούμε να κάνουμε κανονικές ενέργειες.

Για να φτιάχνετε τα δικά σας αρθρώματα
Για να φτιάχνετε τα δικά σας αρθρώματα είναι εύκολο, το έχετε κάνει ήδη, κι αυτό διότι κάθε πρόγραμμα στη Python είναι επίσης κι ένα άρθρωμα. Για το μόνο που πρέπει να είστε σίγουροι είναι να έχει μια επέκταση .py. Το ακόλουθο παράδειγμα θα το κάνει πιο ξεκάθαρο.

Παράδειγμα:
#!/usr/bin/python
# Filename: mymodule.py
def sayhi():
print('Hi, this is mymodule speaking.')
__version__ = '0.1'
# End of mymodule.py

Το ανωτέρω είναι ένα δείγμα αρθρώματος. Οπως μπορείτε να δείτε δεν υπάρχει καμμία ιδιαιτερότητα συγκρινόμενο με ένα συνηθισμένο πρόγραμμα σε Python. Kατόπιν θα δούμε πως να χρησιμοποιούμε αυτό το άρθρωμα στα δικά μας προγράμματα σε Python. Θυμηθείτε ότι το άρθρωμα πρέπει να τοποθετείται στον ίδιο κατάλογο με το πρόγραμμα που το εισάγουμε, ή το άρθρωμα πρέπει να βρίσκεται σε έναν από τους καταλόγους που είναι στη λίστα του sys.path.

Παράδειγμα:
#!/usr/bin/python
# Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print ('Version', mymodule.__version__)

Aποτέλεσμα:
$ python mymodule_demo.py
Hi, this is mymodule speaking.
Version 0.1

Παρατηρήστε ότι χρησιμοποιούμε τον ίδιο συμβολισμό με τελείες για να εισάγουμε μέλη στο άρθρωμα. Η Python κάνει σωστή επαναχρησιμοποίηση του ίδιου συμβολισμού για να του δώσει την χαρακτηριστική Pythόνια αίσθηση, έτσι ώστε να μην χρειάζεται να μαθαίνετε καινούργιους τρόπους για να κάνετε πράγματα.
Εδώ είναι μια έκδοση σύνταξης όπου γίνεται χρήση του from...import:

#!/usr/bin/python
# Filename: mymodule_demo2.py
from mymodule import sayhi, __version__
sayhi()
print('Version', __version__)

Tο αποτέλεσμα του mymodule_demo2.py είναι το ίδιο με το αποτέλεσμα του mymodule_demo.py. Παρατηρήστε ότι εάν ήταν ήδη δηλωμένο το όνομα της έκδοσης (__version__name) στο άρθρωμα αυτό εισάγει mymodule, θα γινόταν σύγκρουση. Αυτό είναι επίσης πιθανόν διότι είναι κοινή πρακτική για κάθε άρθρωμα να δηλώνει το νούμερο έκδοσής του χρησιμοποιώντας αυτό το όνομα. Γι' αυτό το λόγο συνιστάται να προτιμάτε την εντολή import ακόμα κι αν έτσι γίνεται το πρόγραμμά σας μεγαλύτερο.
Επίσης θα μπορούσατε να χρησιμοποιήσετε: from mymodule import *
Αυτό θα εισήγαγε όλα τα δημόσια ονόματα όπως το sayhi αλλά δεν θα εισήγαγε το __version__ επειδή αρχίζει με διπλές κάτω παύλες.
Το Zen της Python:Μια από τις αρχές καθοδήγησης της Python είναι ότι “ Οι ρητές εντολές είναι καλύτερες από αυτές που εξυπακούονται” (Explicit is better than Implicit). Τρέξτε import this για να μάθετε περισσότερα και δείτε αυτή τη συζήτηση ( http://stackoverflow.com/questions/228181/zen-of-python) που παραθέτει παραδείγματα για κάθε μια από τις αρχές της Python.

Η συνάρτηση dir
Μπορείτε να χρησιμοποιήσετε την ενσωματωμένη συνάρτηση dir για να βάλετε σε λίστα τα αναγνωριστικά που ορίζει ένα αντικείμενο. Για παράδειγμα, σε ένα άρθρωμα, τα αναγνωριστικά περιλαμβάνουν τις συναρτήσεις, τις κλάσεις και τις μεταβλητές που ορίζονται σε αυτό το άρθρωμα. Οταν δίνεις ένα όνομα σε ένα άρθρωμα στη συνάρτηση dir(), αυτό επιστρέφει τη λίστα των ονομάτων που ορίζονται σε αυτό το άρθρωμα. Οταν κανένα όρισμα δεν επιθέτεται σε αυτό, τότε επιστρέφει η λίστα των ονομάτων που ορίζονται στο τρέχον άρθρωμα.

Παράδειγμα:
$ python
>>> import sys # παίρνει λίστα των ιδιοχαρακτηριστικών, σε αυτή την περίπτωση, για το άρθρωμα sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__',
'__package__', '__s
tderr__', '__stdin__', '__stdout__', '_clear_type_cache',
'_compact_freelists',
'_current_frames', '_getframe', 'api_version', 'argv',
'builtin_module_names', '
byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook',
'dllhandle'
, 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
'executable',
'exit', 'flags', 'float_info', 'getcheckinterval',
'getdefaultencoding', 'getfil
esystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount',
'getsizeof',
'gettrace', 'getwindowsversion', 'hexversion', 'intern', 'maxsize',
'maxunicode
', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache',
'platfor
m', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setprofile',
'setrecursionlimit
', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version',
'version_in
fo', 'warnoptions', 'winver']
>>> dir() # παίρνει λίστα των χαρακτηριστικών για το τρέχον άρθρωμα
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>> a = 5 # δημιουργεί μια νέα μεταβλητή 'a'
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'sys']
>>> del a # διαγράφει ή μετακινεί ένα όνομα
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'sys']
>>>

Πως λειτουργεί:
Αρχικά βλέπουμε τη χρήση της συνάρτησης dir στο εισαχθέν άρθρωμα sys. Mπορούμε να δούμε την τεράστια λίστα των ιδιοχαρακτηριστικών που περιέχει. Κατόπιν χρησιμοποιούμε την συνάρτηση dir χωρίς να της περάσουμε παραμέτρους. Προκαθορισμένα, επιστρέφει τη λίστα του τρέχοντος αρθρώματος. Παρατηρήστε ότι η λίστα των εισαχθέντων αρθρωμάτων είναι επίσης μέρος αυτής της λίστας. Για να δείτε σε δράση τη dir, ορίζουμε μια καινούργια μεταβλητή a και εκχωρούμε σ'αυτή μια τιμή, μετά τσεκάρουμε τη dir και παρατηρούμε ότι υπάρχει μια επιπρόσθετη τιμή στη λίστα του ίδιου ονόματος. Μετακινούμε τη μεταβλητή/ιδιοχαρακτηριστικό του τρέχοντος αρθρώματος χρησιμοποιώντας την εντολή del και η αλλαγή αντανακλάται πάλι στο αποτέλεσμα της συνάρτησης dir.
Ενα σχόλιο στην del - αυτή η εντολή χρησιμοποιείται για να διαγράψει μια μεταβλητή/όνομα και αφού η εντολή έχει τρέξει, σε αυτή την περίπτωση del a, δεν μπορείτε πια να εισάγετε τη μεταβλητή a – είναι σαν να μην υπήρξε πριν καθόλου.
Σημειώστε επίσης ότι η συνάρτηση dir () λειτουργεί σε οποιοδήποτε αντικείμενο. Για παράδειγμα, τρέξτε dir(print) για να μάθετε σχετικά με τα ιδιοχαρακτηριστικά της συνάρτησης print, ή dir(str) για τα ιδιοχαρακτηριστικά της τάξης str.

Πακέτα(Packages)
Mέχρι τώρα, πρέπει να έχετε αρχίσει να παρατηρείται την ιεραρχία με την οποία οργανώνονται τα προγράμματά σας. Οι μεταβλητές συνήθως πηγαίνουν μέσα στις συναρτήσεις. Οι συναρτήσεις και οι καθολικές μεταβλητές συνήθως πηγαίνουν μέσα στα αρθρώματα. Τι θα γινόταν όμως αν θέλατε να οργανώσετε τα αρθρώματα; Γι' αυτό έρχονται εδώ στο προσκήνιο τα πακέτα.
Τα πακέτα είναι απλώς φάκελοι αρθρωμάτων με ένα ειδικό __init__.py αρχείο που δείχνει στην Python ότι αυτός ο φάκελος είναι ειδικός διότι περιέχει αρθρώματα Python. Ας πούμε ότι θέλετε να δημιουργήσετε ένα πακέτο που ονομάζεται 'world' με υποπακέτα που ονομάζονται 'asia', 'africa', κ.τ.λ. και αυτά τα υποπακέτα με τη σειρά τους περιέχουν αρθρώματα όπως 'india', 'madagascar', κ.τ.λ.
Ενα παράδειγμα για το πως θα δομούσες τους φακέλους:
- <some folder present in the sys.path>/
- world/
- __init__.py
- asia/
- __init__.py
- india/
- __init__.py
- foo.py
- africa/
- __init__.py
- madagascar/
__init__.py
bar.py

Tα πακέτα είναι μια ευκολία στην ιεραρχική οργάνωση των αρθρωμάτων. Θα δείτε πολλά τέτοια παραδείγματα στην κύρια βιβλιοθήκη.

Σύνοψη
Οπως ακριβώς και οι συναρτήσεις είναι επαναχρησιμοποιούμενα μέρη προγραμμάτων, τα αρθρώματα είναι επαναχρησιμοποιούμενα προγράμματα. Τα πακέτα είναι μια άλλη ιεραρχία για να οργανώνετε αρθρώματα. Η κύρια βιβλιοθήκη που συνοδεύει την Python είναι ένα παράδειγμα τέτοιων πακέτων και αρθρωμάτων.
Εχουμε δει πως να χρησιμοποιούμε αυτά τα αρθρώματα και πως να δημιουργούμε δικά μας αρθρώματα.
Κατόπιν θα μάθουμε μερικές ενδιαφέρουσες έννοιες που ονομάζονται δομές δεδομένων.