From b6360369a5722195be6f159c379b35fd32c762be Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sun, 26 Jul 2009 15:11:19 -0400 Subject: [PATCH] further porting --- dip2 | 112 ----------------------------------------------------------- 1 file changed, 112 deletions(-) diff --git a/dip2 b/dip2 index 59210bb..180420c 100755 --- a/dip2 +++ b/dip2 @@ -1,115 +1,3 @@ -

2.4. Everything Is an Object

-

2.6. Testing Modules

-

Python modules are objects and have several useful attributes. You can use this to easily test your modules as you write them. - Here's an example that uses the if __name__ trick. -

-if __name__ == "__main__":

Some quick observations before you get to the good stuff. First, parentheses are not required around the if expression. Second, the if statement ends with a colon, and is followed by indented code. - - -
NoteLike C, Python uses == for comparison and = for assignment. Unlike C, Python does not support in-line assignment, so there's no chance of accidentally assigning the value you thought you were comparing. -

So why is this particular if statement a trick? Modules are objects, and all modules have a built-in attribute __name__. A module's __name__ depends on how you're using the module. If you import the module, then __name__ is the module's filename, without a directory path or file extension. But you can also run the module directly as a standalone -program, in which case __name__ will be a special default value, __main__. -

>>> import odbchelper
->>> odbchelper.__name__
-'odbchelper'

Knowing this, you can design a test suite for your module within the module itself by putting it in this if statement. When you run the module directly, __name__ is __main__, so the test suite executes. When you import the module, __name__ is something else, so the test suite is ignored. This makes it easier to develop and debug new modules before integrating -them into a larger program. - - -
TipOn MacPython, there is an additional step to make the if __name__ trick work. Pop up the module's options menu by clicking the black triangle in the upper-right corner of the window, and - make sure Run as __main__ is checked. -
-

Further Reading on Importing Modules

- - - - - - - - - - - - - -

3.4. Declaring variables

-

Now that you know something about dictionaries, tuples, and lists (oh my!), let's get back to the sample program from Chapter 2, odbchelper.py. -

Python has local and global variables like most other languages, but it has no explicit variable declarations. Variables spring - into existence by being assigned a value, and they are automatically destroyed when they go out of scope. -

Example 3.17. Defining the myParams Variable


-if __name__ == "__main__":
-    myParams = {"server":"mpilgrim", \
-                "database":"master", \
-                "uid":"sa", \
-                "pwd":"secret" \
-                }

Notice the indentation. An if statement is a code block and needs to be indented just like a function. -

Also notice that the variable assignment is one command split over several lines, with a backslash (“\”) serving as a line-continuation marker. - - -
NoteWhen a command is split among several lines with the line-continuation marker (“\”), the continued lines can be indented in any manner; Python's normally stringent indentation rules do not apply. If your Python IDE auto-indents the continued line, you should probably accept its default unless you have a burning reason not to. -

Strictly speaking, expressions in parentheses, straight brackets, or curly braces (like defining a dictionary) can be split into multiple lines with or without the line continuation character (“\”). I like to include the backslash even when it's not required because I think it makes the code easier to read, but that's -a matter of style. - - - - - -[unbound variable exception example was here] - - - - - - -

3.4.2. Assigning Multiple Values at Once

-

One of the cooler programming shortcuts in Python is using sequences to assign multiple values at once. -

Example 3.19. Assigning multiple values at once

>>> v = ('a', 'b', 'e')
->>> (x, y, z) = v     
->>> x
-'a'
->>> y
-'b'
->>> z
-'e'
-
    -
  1. v is a tuple of three elements, and (x, y, z) is a tuple of three variables. Assigning one to the other assigns each of the values of v to each of the variables, in order. -

    This has all sorts of uses. I often want to assign names to a range of values. In C, you would use enum and manually list each constant and its associated value, which seems especially tedious when the values are consecutive. - In Python, you can use the built-in range function with multi-variable assignment to quickly assign consecutive values. -

    Example 3.20. Assigning Consecutive Values

    >>> range(7)              
    -[0, 1, 2, 3, 4, 5, 6]
    ->>> (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7) 
    ->>> MONDAY                
    -0
    ->>> TUESDAY
    -1
    ->>> SUNDAY
    -6
    -
      -
    1. The built-in range function returns a list of integers. In its simplest form, it takes an upper limit and returns a zero-based list counting - up to but not including the upper limit. (If you like, you can pass other parameters to specify a base other than 0 and a step other than 1. You can print range.__doc__ for details.) -
    2. MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are the variables you're defining. (This example came from the calendar module, a fun little module that prints calendars, like the UNIX program cal. The calendar module defines integer constants for days of the week.) -
    3. Now each variable has its value: MONDAY is 0, TUESDAY is 1, and so forth. -

      You can also use multi-variable assignment to build functions that return multiple values, simply by returning a tuple of - all the values. The caller can treat it as a tuple, or assign the values to individual variables. Many standard Python libraries do this, including the os module, which you'll discuss in Chapter 6. -

      -

      Further Reading on Variables

      - - - - - - - - -

      Example 6.12. Introducing sys.modules

      >>> import sys        
       >>> print '\n'.join(sys.modules.keys()) 
       win32api