See All Titles |
![]() ![]() ModulesModules are a logical way to physically organize and distinguish related pieces of Python code into individual files. Modules can contain executable code, functions, classes, or any and all of the above. When you create a Python source file, the name of the module is the same as the file except without the trailing ".py" extension. Once a module is created, you may "import" that module for use from another module using the import statement. How to Import a Moduleimport module_name How to Call a Module Function or Access a Module VariableOnce imported, a module's attributes (functions and variables) can be accessed using the familiar dotted attribute notation: module.function() module.variable We will now present our Hello World! example again, but using the output functions inside the sys module. >>> import sys >>> sys.stdout.write('Hello World!\n') Hello World! This code behaves just like our original Hello World! using the print statement. The only difference is that the standard output write() method is called, and the NEWLINE character needs to be stated explicitly because, unlike the print statement, write() does not do that for you. Let us now look at some other attributes of the sys module and some of the functions in the string module as well. >>> import sys >>> import string >>> sys.platform 'win32' >>> sys.version '1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)]' >>> >>> up2space = string.find(sys.version, ' ') >>> ver = sys.version[:up2space] >>> ver 1.5.2 >>> >>> print 'I am running Python %s on %s' % (ver, sys.platform) I am running Python 1.5.2 on win32 As you can probably surmise, the sys.platform and sys.version variables contain information regarding the system you are running on and which version of the Python interpreter you are using. The string.find() function looks for substrings in strings. In the example, we are capturing just the version number which occurs from the beginning of the string to right before the first space character. We use find() to tell us where the space is located so we can grab all the characters in the string before the space. Another way to snare the version number is by breaking up the entire string into words (separated by spaces). The version number is the first word, so that is all we want. The string.split() function returns a list of all the "words" in a string: >>> verchunks = string.split(sys.version) >>> verchunks ['1.5.2', '(#0,', 'Apr', '13', '1999,', '10:51:12)', '[MSC', '32', 'bit', '(Intel)]'] >>> print 'I am running Python %s on %s' % \ … (verchunks[0], sys.platform) I am running Python 1.5.2 on win32 Our output is the exact same as the example above In this case, there was clearly more than one way to accomplish the same task. This is not always the case in Python, but both examples will allow the reader to decide on the appropriate course of action when warranted. You can find out more information on modules and importing in Chapter 12. We will cover all of the above topics in much greater detail throughout the text, but hopefully we have provided enough of a "quick dip in the pool" to facilitate your needs if your primary goal is to get started working with Python as quickly as possible without too much serious reading.
|
© 2002, O'Reilly & Associates, Inc. |