From 62aa447dafef6b4a4af86eef9d024c6f1d479859 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Mon, 27 Jul 2009 06:14:51 -0400 Subject: [PATCH] moved #importsearchpath up, fixed directory references, plus a few typos --- your-first-python-program.html | 65 +++++++++++++++++----------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/your-first-python-program.html b/your-first-python-program.html index 5ece0ec..489b1b2 100755 --- a/your-first-python-program.html +++ b/your-first-python-program.html @@ -103,7 +103,7 @@ if __name__ == '__main__': print(approximate_size(1000000000000, False)) print(approximate_size(1000000000000))
    -
  1. This calls the approximate_size() function with two argument. Within the approximate_size() function, a_kilobyte_is_1024_bytes will be False, since you explicitly passed False as the second argument. +
  2. This calls the approximate_size() function with two arguments. Within the approximate_size() function, a_kilobyte_is_1024_bytes will be False, since you explicitly passed False as the second argument.
  3. This calls the approximate_size() function with only one argument. But that’s OK, because the second argument is optional! Since the caller doesn’t specify, the second argument defaults to True, as defined by the function declaration.
@@ -159,6 +159,38 @@ SyntaxError: non-keyword arg after keyword arg

⁂ +

The import Search Path

+

Before this goes any further, I want to briefly mention the library search path. Python looks in several places when you try to import a module. Specifically, it looks in all the directories defined in sys.path. This is just a list, and you can easily view it or modify it with standard list methods. (You’ll learn more about lists in Native Datatypes.) +

+>>> import sys                                                 
+>>> sys.path                                                   
+['', 
+ '/usr/lib/python31.zip', 
+ '/usr/lib/python3.1',
+ '/usr/lib/python3.1/plat-linux2@EXTRAMACHDEPPATH@', 
+ '/usr/lib/python3.1/lib-dynload', 
+ '/usr/lib/python3.1/dist-packages', 
+ '/usr/local/lib/python3.1/dist-packages']
+>>> sys                                                        
+<module 'sys' (built-in)>
+>>> sys.path.insert(0, '/home/mark/diveintopython3/examples')  
+>>> sys.path                                                   
+['/home/mark/py', 
+ '', 
+ '/usr/lib/python30.zip', 
+ '/usr/lib/python3.0', 
+ '/usr/lib/python3.0/plat-linux2@EXTRAMACHDEPPATH@', 
+ '/usr/lib/python3.0/lib-dynload', 
+ '/usr/lib/python3.0/dist-packages', 
+ '/usr/local/lib/python3.0/dist-packages']
+
    +
  1. Importing the sys module makes all of its functions and attributes available. +
  2. sys.path is a list of directory names that constitute the current search path. (Yours will look different, depending on your operating system, what version of Python you’re running, and where it was originally installed.) Python will look through these directories (in this order) for a .py file whose name matches what you’re trying to import. +
  3. Actually, I lied; the truth is more complicated than that, because not all modules are stored as .py files. Some, like the sys module, are built-in modules; they are actually baked right into Python itself. Built-in modules behave just like regular modules, but their Python source code is not available, because they are not written in Python! (The sys module is written in C.) +
  4. You can add a new directory to Python’s search path at runtime by adding the directory name to sys.path, and then Python will look in that directory as well, whenever you try to import a module. The effect lasts as long as Python is running. +
  5. By using sys.path.insert(0, new_path), you inserted a new directory as the first item of the sys.path list, and therefore at the beginning of Python’s search path. This is almost always what you want. In case of naming conflicts (for example, if Python ships with version 2 of a particular library but you want to use version 3), this ensures that your modules will be found and used instead of the modules that came with Python. +
+

Everything Is An Object

In case you missed it, I just said that Python functions have attributes, and that those attributes are available at runtime. A function, like everything else in Python, is an object.

Run the interactive Python shell and follow along: @@ -185,37 +217,6 @@ SyntaxError: non-keyword arg after keyword arg

import in Python is like require in Perl. Once you import a Python module, you access its functions with module.function; once you require a Perl module, you access its functions with module::function.

-

The import Search Path

-

Before this goes any further, I want to briefly mention the library search path. Python looks in several places when you try to import a module. Specifically, it looks in all the directories defined in sys.path. This is just a list, and you can easily view it or modify it with standard list methods. (You’ll learn more about lists in Native Datatypes.) -

->>> import sys                           
->>> sys.path                             
-['', 
- '/usr/lib/python30.zip', 
- '/usr/lib/python3.0',
- '/usr/lib/python3.0/plat-linux2@EXTRAMACHDEPPATH@', 
- '/usr/lib/python3.0/lib-dynload', 
- '/usr/lib/python3.0/dist-packages', 
- '/usr/local/lib/python3.0/dist-packages']
->>> sys                                  
-<module 'sys' (built-in)>
->>> sys.path.insert(0, '/home/mark/py')  
->>> sys.path                             
-['/home/mark/py', 
- '', 
- '/usr/lib/python30.zip', 
- '/usr/lib/python3.0', 
- '/usr/lib/python3.0/plat-linux2@EXTRAMACHDEPPATH@', 
- '/usr/lib/python3.0/lib-dynload', 
- '/usr/lib/python3.0/dist-packages', 
- '/usr/local/lib/python3.0/dist-packages']
-
    -
  1. Importing the sys module makes all of its functions and attributes available. -
  2. sys.path is a list of directory names that constitute the current search path. (Yours will look different, depending on your operating system, what version of Python you’re running, and where it was originally installed.) Python will look through these directories (in this order) for a .py file whose name matches what you’re trying to import. -
  3. Actually, I lied; the truth is more complicated than that, because not all modules are stored as .py files. Some, like the sys module, are built-in modules; they are actually baked right into Python itself. Built-in modules behave just like regular modules, but their Python source code is not available, because they are not written in Python! (The sys module is written in C.) -
  4. You can add a new directory to Python’s search path at runtime by adding the directory name to sys.path, and then Python will look in that directory as well, whenever you try to import a module. The effect lasts as long as Python is running. -
  5. By using sys.path.insert(0, new_path), you inserted a new directory as the first item of the sys.path list, and therefore at the beginning of Python’s search path. This is almost always what you want. In case of naming conflicts (for example, if Python ships with version 2 of a particular library but you want to use version 3), this ensures that your modules will be found and used instead of the modules that came with Python. -

What’s An Object?

Everything in Python is an object, and everything can have attributes and methods. All functions have a built-in attribute __doc__, which returns the docstring defined in the function’s source code. The sys module is an object which has (among other things) an attribute called path. And so forth.

Still, this doesn’t answer the more fundamental question: what is an object? Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser. Some objects have neither attributes nor methods, but they could. Not all objects are subclassable. But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function.