DekGenius.com
[ Team LiB ] Previous Section Next Section

B.1 Part I, Getting Started

See Section 3.13 for the exercises.

  1. Interaction. Assuming Python is configured properly, interaction should look something like the following. You can run this any way you like: in IDLE, from a shell prompt, and so on:

    % python
    ...copyright information lines...
    >>> "Hello World!"
    'Hello World!'
    >>>                     # Ctrl-D or Ctrl-Z to exit, or window close
  2. Programs. Your code (i.e., module) file module1.py and shell interactions should look like:

    print 'Hello module world!'
    
    % python module1.py
    Hello module world!

    Again, feel free to run this other ways—by clicking its icon, by IDLE's Edit/RunScript menu option, and so on.

  3. Modules. The following interaction listing illustrates running a module file by importing it.

    % python
    >>> import module1
    Hello module world!
    >>>

    Remember that you need to reload the module to run again without stopping and restarting the interpreter. The questions about moving the file to a different directory and importing it again is a trick question: if Python generates a module1.pyc file in the original directory, it uses that when you import the module, even if the source code file (.py) has been moved to a directory not on Python's search path. The .pyc file is written automatically if Python has access to the source file's directory and contains the compiled byte-code version of a module. See Part V for more on modules.

  4. Scripts. Assuming your platform supports the #! trick, your solution will look like the following (although your #! line may need to list another path on your machine):

    #!/usr/local/bin/python          (or #!/usr/bin/env python)
    print 'Hello module world!'
    
    % chmod +x module1.py
    
    % module1.py
    Hello module world!
  5. Errors. The interaction below demonstrates the sort of error messages you get when you complete this exercise. Really, you're triggering Python exceptions; the default exception handling behavior terminates the running Python program and prints an error message and stack trace on the screen. The stack trace shows where you were in a program when the exception occurred. In Part VII, you will learn that you can catch exceptions using try statements and process them arbitrarily; you'll also see that Python includes a full-blown source code debugger for special error detection requirements. For now, notice that Python gives meaningful messages when programming errors occur (instead of crashing silently):

    % python
    >>> 1 / 0
    Traceback (innermost last):
      File "<stdin>", line 1, in ?
    ZeroDivisionError: integer division or modulo
    >>>
    >>> x
    Traceback (innermost last):
      File "<stdin>", line 1, in ?
    NameError: x
  6. Breaks. When you type this code:

    L = [1, 2]
    L.append(L)

    you create a cyclic data structure in Python. In Python releases before Version 1.5.1, the Python printer wasn't smart enough to detect cycles in objects, and it would print an unending stream of [1, 2, [1, 2, [1, 2, [1, 2,—and so on, until you hit the break key combination on your machine (which, technically, raises a keyboard-interrupt exception that prints a default message). Beginning with Python Version 1.5.1, the printer is clever enough to detect cycles and prints [[...]] instead.

    The reason for the cycle is subtle and requires information you will gain in Part II. But in short, assignment in Python always generates references to objects (which you can think of as implicitly followed pointers). When you run the first assignment above, the name L becomes a named reference to a two-item list object. Python lists are really arrays of object references, with an append method that changes the array in place by tacking on another object reference. Here, the append call adds a reference to the front of L at the end of L, which leads to the cycle illustrated in Figure B-1. Believe it or not, cyclic data structures can sometimes be useful (but not when printed!).

Figure B-1. A cyclic list
figs/lpy2_ab01.gif
    [ Team LiB ] Previous Section Next Section