# modules.py # Namespaces # # Namespace is a concept used in programming languages. As we create # functions, thus function names, we may want to create the same function # name to mean differnt things in different contexts. For example, count # may mean counting students or beans depending on which program you are # dealing with. Rather than naming them count_students and count_beans # we may want to name them both count, but in a diffent context. We create # a namespace in which names are contained. So if we have a namespace that # contains all the names that deal with student-related and another containing # all the names that deal with beans, then we can use the same name, e.g., # count as long as we are confined in a namespace. If we want to refer to a # name in one namespace from another namespace, we can devise a syntax to do # that as well. Namespace is a concept used to manage names in a programming # language. After all, a program consists of a bunch of names such as variable # names, function names, etc. When there are many names, it is easier to deal # with them if we partition them into differnt spaces. Python like many other # languages allows this mechanism and it is implemented by modules. # Modules # # A module is a file containing Python definitions that are intended to be # used in other Python programs. There are many Python modules that come # with Python as part of the standard library. We have seen two of these # already: the string module and the math module. # Let us learn how to create a module first. All we need to create a module # is a Python program file with a .py extension on the filename. For example, # functions.py that I created is one such module. # # Let us create one as follows: we will create a file named seqtools.py that # contains one definition named remove_at. I will create it as a separate file # named seqtools.py. # # see seqtools.py # Now, that we created a module, let us learn how to use it. # # Start a Python session using the shell mode, and try the following. I am # showing you an exact interaction: # # >>> from seqtools import remove_at # >>> s = "This is fun!" # >>> remove_at(3, s) # 'Thi is fun!' # # As you can see, we are using the 'from' and 'import' commands to import # a specific name (remove_at) from a module (seqtools). In this particular # situation we are importing the name into the current module, which is the # top-level, interactive module. Once you import it, you can use the imported # name as if the name was created in your own module. One bad thing is that # if you already had the same name in your own module, the old one will be # lost. To avoid this situation, there is another way of importing that I # will show you now. # Here again I am showing you an interactive session using the shell mode to # illustrate how it is done, like this: # # >>> import seqtools # >>> s = "This is fun!" # >>> seqtools.remove_at(3, s) # 'Thi is fun!' # # As you can see here we are importing everything from the module seqtools. # Note that we don't say 'import seqtools.py', but 'import seqtools' without # the file extension (.py). Since there is only one definition in seqtools, # only one definition is imported, but if there were many, all of those would # be imported. If we import it this way, we must specify the module name # along with the name in the module if we want to use a name in the imported # module. We use the dot ('.') operator to separate the module name and a # name in the module, e.g., seqtools.remove_name. Suppose there was already # a definition named remove_name in the current module. Then, remove_name # alone would refer to the one in the current module, and seqtools.remove_at # would refer to the one from seqtools module. Note that this works because # we have the file seqtools.py located in the same folder where the current # python session was started. If the module to be imported is located in a # different folder, we would have do to a little more work. I will explain # that later in this file. # # Some programming languages use packages to mean modules. I may use that # term inadvertantly in the future, and you will know what I mean if I do. # We can use 'from modulename import *' to mean import everything from the # named module, e.g., # # >>> from seqtools import * # # This would import all the names from seqtools into the current module thus # ocerwriting any exsisting names if there are any names that are same in the # current module as the ones imported from seqtools. # # So, what would we have to do if the module to be imported is located in a # different folder than the folder where the current python session was # started? Well, if the module file is located in one of the folders that is # included in the current 'path' for the python session, then it is okay. If # not, we will have do some more work. So, how do we find out if the folder # where the module to be imported is located is in the path or not? Try this # in your current python session: # # >>> import sys # >>> sys.path # # The value of sys.path is returned and it will show all the folders that are # included in the path. If the folder where the module is located is in the # the path, you are done. If not we have to include that folder in the path. # Assuming that the module is located in the folder: /Users/alee/python, this # is what we would do: # # >>> import os # >>> os.path.join ## Just to see if join is available # # >>> sys.path # ['', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload', '/Library/Python/2.5/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC'] # >>> sys.path.append(os.path.join("/Users", "alee", "python")) # >>> sys.path # ['', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload', '/Library/Python/2.5/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC', '/Users/alee/python'] # >>> import seqtools # >>> s = "Thisisfun..." # >>> seqtools.remove_at(3, s) # 'Thiisfun...' # # Note that remove_at(3, s) without seqtools. would not work. However, if you # imported it using 'from seqtools import *' then it remove_at(3, s) alone would # work. # # I can encapsulate all this stuff I just did to add something into the path in # a file called init.py and you may use it every time you want to do something # like that. See init.py to see what I did. With the init.py available, next # time just do the following right after you start a new python session: # # >>> from init import * # # or # # >>> import init # # and then continue with whatever you wanted to do in your current python # session. # There are a few more subtlties with modules and you may refer to Section # 10.4 of [DEM] if you want to learn more about modules. #