You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

572 lines
23 KiB

Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line Change to tutorial wording for reading text / binary files on Windows. Issue #6301. ........ r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567. ........ r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712. ........ r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line Tutorial tweaks. Issue 6849. ........ r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866. ........ r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line Typo fix. ........ r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568. ........ r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line Remove an extraneous space in unittest documentation. ........ r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line #6908: fix association of hashlib hash attributes. ........ r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line Py_SetPythonHome uses static storage #6913 ........ r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line Add Armin Ronacher. ........ r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line kill bare except ........ r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line pep 8 defaults ........ r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line rationalize a bit ........ r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line #6932: remove paragraph that advises relying on __del__ being called. ........ r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line use macros ........ r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line #6936: for interactive use, quit() is just fine. ........ r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line #6905: use better exception messages in inspect when the argument is of the wrong type. ........ r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line #6938: "ident" is always a string, so use a format code which works. ........ r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line Use str.format() to fix beginner's mistake with %-style string formatting. ........ r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line Optimize optimization and fix method name in docstring. ........ r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line #6925: rewrite docs for locals() and vars() a bit. ........ r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line #6930: clarify description about byteorder handling in UTF decoder routines. ........ r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line #6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple. ........ r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line Update bug tracker reference. ........ r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line #6946: fix duplicate index entries for datetime classes. ........ r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line Fix references to threading.enumerate(). ........ r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line Add Doug. ........ r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line Add Mark Summerfield. ........ r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line Fix encoding name. ........ r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line Fixed a typo, and added sections on optimization and using arbitrary objects as messages. ........ r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line fix typos/rephrase ........ r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line comment out ugly xxx ........ r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line Tidied up name of parameter in StreamHandler ........ r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956. ........ r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line Documentation improvement for load_tests protocol in unittest. Issue 6515. ........ r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines Patch from Thomas Barr so that csv.Sniffer will set doublequote property. Closes issue 6606. ........ r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line Issue #7014: logging: Improved IronPython 2.6 compatibility. ........ r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines Issue 7008: Better document str.title and show how to work around the apostrophe problem. ........ r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line test logging ........
17 years ago
Merged revisions 62914-62916,62918-62919,62921-62922,62924-62942,62944-62945,62947-62949 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62914 | skip.montanaro | 2008-05-08 20:45:00 -0400 (Thu, 08 May 2008) | 4 lines Add an example about using NamedTemporaryFile() to replace mktemp(). I'm unclear whether the verbatim text should have been indented or by how much. ........ r62915 | benjamin.peterson | 2008-05-08 20:50:40 -0400 (Thu, 08 May 2008) | 2 lines reindent example ........ r62927 | georg.brandl | 2008-05-09 02:09:25 -0400 (Fri, 09 May 2008) | 2 lines #2788: add .hgignore file. ........ r62928 | georg.brandl | 2008-05-09 02:10:43 -0400 (Fri, 09 May 2008) | 2 lines #2781: fix function name. ........ r62929 | georg.brandl | 2008-05-09 02:18:27 -0400 (Fri, 09 May 2008) | 2 lines Add a sentence to basicConfig() that is in the docstring. ........ r62930 | georg.brandl | 2008-05-09 02:26:54 -0400 (Fri, 09 May 2008) | 2 lines Add another link to colorsys docs. ........ r62931 | georg.brandl | 2008-05-09 02:36:07 -0400 (Fri, 09 May 2008) | 2 lines Add Kodos as a re reference. ........ r62932 | georg.brandl | 2008-05-09 02:39:58 -0400 (Fri, 09 May 2008) | 2 lines Add a note about using reload(). ........ r62933 | andrew.kuchling | 2008-05-09 07:46:05 -0400 (Fri, 09 May 2008) | 3 lines Update planned release date. Uncomment PEP 370 section. Add some module items ........ r62934 | christian.heimes | 2008-05-09 08:19:09 -0400 (Fri, 09 May 2008) | 1 line Add --user option to build_ext ........ r62948 | mark.dickinson | 2008-05-09 13:54:23 -0400 (Fri, 09 May 2008) | 3 lines Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and negative; fix to return an (appropriately signed) zero instead. ........ r62949 | martin.v.loewis | 2008-05-09 14:21:55 -0400 (Fri, 09 May 2008) | 1 line Use the CHM file name that Sphinx assigns. ........
18 years ago
Merged revisions 62914-62916,62918-62919,62921-62922,62924-62942,62944-62945,62947-62949 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62914 | skip.montanaro | 2008-05-08 20:45:00 -0400 (Thu, 08 May 2008) | 4 lines Add an example about using NamedTemporaryFile() to replace mktemp(). I'm unclear whether the verbatim text should have been indented or by how much. ........ r62915 | benjamin.peterson | 2008-05-08 20:50:40 -0400 (Thu, 08 May 2008) | 2 lines reindent example ........ r62927 | georg.brandl | 2008-05-09 02:09:25 -0400 (Fri, 09 May 2008) | 2 lines #2788: add .hgignore file. ........ r62928 | georg.brandl | 2008-05-09 02:10:43 -0400 (Fri, 09 May 2008) | 2 lines #2781: fix function name. ........ r62929 | georg.brandl | 2008-05-09 02:18:27 -0400 (Fri, 09 May 2008) | 2 lines Add a sentence to basicConfig() that is in the docstring. ........ r62930 | georg.brandl | 2008-05-09 02:26:54 -0400 (Fri, 09 May 2008) | 2 lines Add another link to colorsys docs. ........ r62931 | georg.brandl | 2008-05-09 02:36:07 -0400 (Fri, 09 May 2008) | 2 lines Add Kodos as a re reference. ........ r62932 | georg.brandl | 2008-05-09 02:39:58 -0400 (Fri, 09 May 2008) | 2 lines Add a note about using reload(). ........ r62933 | andrew.kuchling | 2008-05-09 07:46:05 -0400 (Fri, 09 May 2008) | 3 lines Update planned release date. Uncomment PEP 370 section. Add some module items ........ r62934 | christian.heimes | 2008-05-09 08:19:09 -0400 (Fri, 09 May 2008) | 1 line Add --user option to build_ext ........ r62948 | mark.dickinson | 2008-05-09 13:54:23 -0400 (Fri, 09 May 2008) | 3 lines Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and negative; fix to return an (appropriately signed) zero instead. ........ r62949 | martin.v.loewis | 2008-05-09 14:21:55 -0400 (Fri, 09 May 2008) | 1 line Use the CHM file name that Sphinx assigns. ........
18 years ago
Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line Change to tutorial wording for reading text / binary files on Windows. Issue #6301. ........ r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567. ........ r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712. ........ r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line Tutorial tweaks. Issue 6849. ........ r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866. ........ r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line Typo fix. ........ r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568. ........ r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line Remove an extraneous space in unittest documentation. ........ r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line #6908: fix association of hashlib hash attributes. ........ r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line Py_SetPythonHome uses static storage #6913 ........ r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line Add Armin Ronacher. ........ r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line kill bare except ........ r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line pep 8 defaults ........ r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line rationalize a bit ........ r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line #6932: remove paragraph that advises relying on __del__ being called. ........ r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line use macros ........ r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line #6936: for interactive use, quit() is just fine. ........ r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line #6905: use better exception messages in inspect when the argument is of the wrong type. ........ r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line #6938: "ident" is always a string, so use a format code which works. ........ r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line Use str.format() to fix beginner's mistake with %-style string formatting. ........ r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line Optimize optimization and fix method name in docstring. ........ r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line #6925: rewrite docs for locals() and vars() a bit. ........ r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line #6930: clarify description about byteorder handling in UTF decoder routines. ........ r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line #6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple. ........ r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line Update bug tracker reference. ........ r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line #6946: fix duplicate index entries for datetime classes. ........ r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line Fix references to threading.enumerate(). ........ r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line Add Doug. ........ r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line Add Mark Summerfield. ........ r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line Fix encoding name. ........ r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line Fixed a typo, and added sections on optimization and using arbitrary objects as messages. ........ r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line fix typos/rephrase ........ r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line comment out ugly xxx ........ r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line Tidied up name of parameter in StreamHandler ........ r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956. ........ r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line Documentation improvement for load_tests protocol in unittest. Issue 6515. ........ r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines Patch from Thomas Barr so that csv.Sniffer will set doublequote property. Closes issue 6606. ........ r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line Issue #7014: logging: Improved IronPython 2.6 compatibility. ........ r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines Issue 7008: Better document str.title and show how to work around the apostrophe problem. ........ r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line test logging ........
17 years ago
Merged revisions 74074,74077,74111,74188,74192-74193,74200,74252-74253,74258-74261 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74074 | georg.brandl | 2009-07-18 05:03:10 -0400 (Sat, 18 Jul 2009) | 1 line #6513: fix example code: warning categories are classes, not instances. ........ r74077 | georg.brandl | 2009-07-18 05:43:40 -0400 (Sat, 18 Jul 2009) | 1 line #6489: fix an ambiguity in getiterator() documentation. ........ r74111 | benjamin.peterson | 2009-07-20 09:30:10 -0400 (Mon, 20 Jul 2009) | 1 line remove docs for deprecated -p option ........ r74188 | benjamin.peterson | 2009-07-23 10:25:31 -0400 (Thu, 23 Jul 2009) | 1 line use bools ........ r74192 | georg.brandl | 2009-07-24 12:28:38 -0400 (Fri, 24 Jul 2009) | 1 line Fix arg types of et#. ........ r74193 | georg.brandl | 2009-07-24 12:46:38 -0400 (Fri, 24 Jul 2009) | 1 line Dont put "void" in signature for nullary functions. ........ r74200 | georg.brandl | 2009-07-25 09:02:15 -0400 (Sat, 25 Jul 2009) | 1 line #6571: add index entries for more operators. ........ r74252 | georg.brandl | 2009-07-29 12:06:31 -0400 (Wed, 29 Jul 2009) | 1 line #6593: fix link targets. ........ r74253 | georg.brandl | 2009-07-29 12:09:17 -0400 (Wed, 29 Jul 2009) | 1 line #6591: add reference to ioctl in fcntl module for platforms other than Windows. ........ r74258 | georg.brandl | 2009-07-29 12:57:05 -0400 (Wed, 29 Jul 2009) | 1 line Add a link to readline, and mention IPython and bpython. ........ r74259 | georg.brandl | 2009-07-29 13:07:21 -0400 (Wed, 29 Jul 2009) | 1 line Fix some markup and small factual glitches found by M. Markert. ........ r74260 | georg.brandl | 2009-07-29 13:15:20 -0400 (Wed, 29 Jul 2009) | 1 line Fix a few markup glitches. ........ r74261 | georg.brandl | 2009-07-29 13:50:25 -0400 (Wed, 29 Jul 2009) | 1 line Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better. ........
17 years ago
Merged revisions 74074,74077,74111,74188,74192-74193,74200,74252-74253,74258-74261 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74074 | georg.brandl | 2009-07-18 05:03:10 -0400 (Sat, 18 Jul 2009) | 1 line #6513: fix example code: warning categories are classes, not instances. ........ r74077 | georg.brandl | 2009-07-18 05:43:40 -0400 (Sat, 18 Jul 2009) | 1 line #6489: fix an ambiguity in getiterator() documentation. ........ r74111 | benjamin.peterson | 2009-07-20 09:30:10 -0400 (Mon, 20 Jul 2009) | 1 line remove docs for deprecated -p option ........ r74188 | benjamin.peterson | 2009-07-23 10:25:31 -0400 (Thu, 23 Jul 2009) | 1 line use bools ........ r74192 | georg.brandl | 2009-07-24 12:28:38 -0400 (Fri, 24 Jul 2009) | 1 line Fix arg types of et#. ........ r74193 | georg.brandl | 2009-07-24 12:46:38 -0400 (Fri, 24 Jul 2009) | 1 line Dont put "void" in signature for nullary functions. ........ r74200 | georg.brandl | 2009-07-25 09:02:15 -0400 (Sat, 25 Jul 2009) | 1 line #6571: add index entries for more operators. ........ r74252 | georg.brandl | 2009-07-29 12:06:31 -0400 (Wed, 29 Jul 2009) | 1 line #6593: fix link targets. ........ r74253 | georg.brandl | 2009-07-29 12:09:17 -0400 (Wed, 29 Jul 2009) | 1 line #6591: add reference to ioctl in fcntl module for platforms other than Windows. ........ r74258 | georg.brandl | 2009-07-29 12:57:05 -0400 (Wed, 29 Jul 2009) | 1 line Add a link to readline, and mention IPython and bpython. ........ r74259 | georg.brandl | 2009-07-29 13:07:21 -0400 (Wed, 29 Jul 2009) | 1 line Fix some markup and small factual glitches found by M. Markert. ........ r74260 | georg.brandl | 2009-07-29 13:15:20 -0400 (Wed, 29 Jul 2009) | 1 line Fix a few markup glitches. ........ r74261 | georg.brandl | 2009-07-29 13:50:25 -0400 (Wed, 29 Jul 2009) | 1 line Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better. ........
17 years ago
Merged revisions 74074,74077,74111,74188,74192-74193,74200,74252-74253,74258-74261 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74074 | georg.brandl | 2009-07-18 05:03:10 -0400 (Sat, 18 Jul 2009) | 1 line #6513: fix example code: warning categories are classes, not instances. ........ r74077 | georg.brandl | 2009-07-18 05:43:40 -0400 (Sat, 18 Jul 2009) | 1 line #6489: fix an ambiguity in getiterator() documentation. ........ r74111 | benjamin.peterson | 2009-07-20 09:30:10 -0400 (Mon, 20 Jul 2009) | 1 line remove docs for deprecated -p option ........ r74188 | benjamin.peterson | 2009-07-23 10:25:31 -0400 (Thu, 23 Jul 2009) | 1 line use bools ........ r74192 | georg.brandl | 2009-07-24 12:28:38 -0400 (Fri, 24 Jul 2009) | 1 line Fix arg types of et#. ........ r74193 | georg.brandl | 2009-07-24 12:46:38 -0400 (Fri, 24 Jul 2009) | 1 line Dont put "void" in signature for nullary functions. ........ r74200 | georg.brandl | 2009-07-25 09:02:15 -0400 (Sat, 25 Jul 2009) | 1 line #6571: add index entries for more operators. ........ r74252 | georg.brandl | 2009-07-29 12:06:31 -0400 (Wed, 29 Jul 2009) | 1 line #6593: fix link targets. ........ r74253 | georg.brandl | 2009-07-29 12:09:17 -0400 (Wed, 29 Jul 2009) | 1 line #6591: add reference to ioctl in fcntl module for platforms other than Windows. ........ r74258 | georg.brandl | 2009-07-29 12:57:05 -0400 (Wed, 29 Jul 2009) | 1 line Add a link to readline, and mention IPython and bpython. ........ r74259 | georg.brandl | 2009-07-29 13:07:21 -0400 (Wed, 29 Jul 2009) | 1 line Fix some markup and small factual glitches found by M. Markert. ........ r74260 | georg.brandl | 2009-07-29 13:15:20 -0400 (Wed, 29 Jul 2009) | 1 line Fix a few markup glitches. ........ r74261 | georg.brandl | 2009-07-29 13:50:25 -0400 (Wed, 29 Jul 2009) | 1 line Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better. ........
17 years ago
Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line Change to tutorial wording for reading text / binary files on Windows. Issue #6301. ........ r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567. ........ r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712. ........ r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line Tutorial tweaks. Issue 6849. ........ r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866. ........ r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line Typo fix. ........ r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568. ........ r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line Remove an extraneous space in unittest documentation. ........ r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line #6908: fix association of hashlib hash attributes. ........ r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line Py_SetPythonHome uses static storage #6913 ........ r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line Add Armin Ronacher. ........ r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line kill bare except ........ r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line pep 8 defaults ........ r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line rationalize a bit ........ r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line #6932: remove paragraph that advises relying on __del__ being called. ........ r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line use macros ........ r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line #6936: for interactive use, quit() is just fine. ........ r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line #6905: use better exception messages in inspect when the argument is of the wrong type. ........ r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line #6938: "ident" is always a string, so use a format code which works. ........ r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line Use str.format() to fix beginner's mistake with %-style string formatting. ........ r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line Optimize optimization and fix method name in docstring. ........ r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line #6925: rewrite docs for locals() and vars() a bit. ........ r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line #6930: clarify description about byteorder handling in UTF decoder routines. ........ r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line #6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple. ........ r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line Update bug tracker reference. ........ r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line #6946: fix duplicate index entries for datetime classes. ........ r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line Fix references to threading.enumerate(). ........ r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line Add Doug. ........ r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line Add Mark Summerfield. ........ r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line Fix encoding name. ........ r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line Fixed a typo, and added sections on optimization and using arbitrary objects as messages. ........ r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line fix typos/rephrase ........ r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line comment out ugly xxx ........ r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line Tidied up name of parameter in StreamHandler ........ r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956. ........ r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line Documentation improvement for load_tests protocol in unittest. Issue 6515. ........ r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines Patch from Thomas Barr so that csv.Sniffer will set doublequote property. Closes issue 6606. ........ r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line Issue #7014: logging: Improved IronPython 2.6 compatibility. ........ r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines Issue 7008: Better document str.title and show how to work around the apostrophe problem. ........ r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line test logging ........
17 years ago
Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line Change to tutorial wording for reading text / binary files on Windows. Issue #6301. ........ r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567. ........ r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712. ........ r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line Tutorial tweaks. Issue 6849. ........ r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866. ........ r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line Typo fix. ........ r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568. ........ r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line Remove an extraneous space in unittest documentation. ........ r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line #6908: fix association of hashlib hash attributes. ........ r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line Py_SetPythonHome uses static storage #6913 ........ r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line Add Armin Ronacher. ........ r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line kill bare except ........ r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line pep 8 defaults ........ r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line rationalize a bit ........ r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line #6932: remove paragraph that advises relying on __del__ being called. ........ r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line use macros ........ r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line #6936: for interactive use, quit() is just fine. ........ r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line #6905: use better exception messages in inspect when the argument is of the wrong type. ........ r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line #6938: "ident" is always a string, so use a format code which works. ........ r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line Use str.format() to fix beginner's mistake with %-style string formatting. ........ r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line Optimize optimization and fix method name in docstring. ........ r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line #6925: rewrite docs for locals() and vars() a bit. ........ r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line #6930: clarify description about byteorder handling in UTF decoder routines. ........ r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line #6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple. ........ r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line Update bug tracker reference. ........ r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line #6946: fix duplicate index entries for datetime classes. ........ r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line Fix references to threading.enumerate(). ........ r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line Add Doug. ........ r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line Add Mark Summerfield. ........ r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line Fix encoding name. ........ r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line Fixed a typo, and added sections on optimization and using arbitrary objects as messages. ........ r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line fix typos/rephrase ........ r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line comment out ugly xxx ........ r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line Tidied up name of parameter in StreamHandler ........ r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956. ........ r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line Documentation improvement for load_tests protocol in unittest. Issue 6515. ........ r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines Patch from Thomas Barr so that csv.Sniffer will set doublequote property. Closes issue 6606. ........ r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line Issue #7014: logging: Improved IronPython 2.6 compatibility. ........ r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines Issue 7008: Better document str.title and show how to work around the apostrophe problem. ........ r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line test logging ........
17 years ago
  1. .. _tut-modules:
  2. *******
  3. Modules
  4. *******
  5. If you quit from the Python interpreter and enter it again, the definitions you
  6. have made (functions and variables) are lost. Therefore, if you want to write a
  7. somewhat longer program, you are better off using a text editor to prepare the
  8. input for the interpreter and running it with that file as input instead. This
  9. is known as creating a *script*. As your program gets longer, you may want to
  10. split it into several files for easier maintenance. You may also want to use a
  11. handy function that you've written in several programs without copying its
  12. definition into each program.
  13. To support this, Python has a way to put definitions in a file and use them in a
  14. script or in an interactive instance of the interpreter. Such a file is called a
  15. *module*; definitions from a module can be *imported* into other modules or into
  16. the *main* module (the collection of variables that you have access to in a
  17. script executed at the top level and in calculator mode).
  18. A module is a file containing Python definitions and statements. The file name
  19. is the module name with the suffix :file:`.py` appended. Within a module, the
  20. module's name (as a string) is available as the value of the global variable
  21. ``__name__``. For instance, use your favorite text editor to create a file
  22. called :file:`fibo.py` in the current directory with the following contents::
  23. # Fibonacci numbers module
  24. def fib(n): # write Fibonacci series up to n
  25. a, b = 0, 1
  26. while a < n:
  27. print(a, end=' ')
  28. a, b = b, a+b
  29. print()
  30. def fib2(n): # return Fibonacci series up to n
  31. result = []
  32. a, b = 0, 1
  33. while a < n:
  34. result.append(a)
  35. a, b = b, a+b
  36. return result
  37. Now enter the Python interpreter and import this module with the following
  38. command::
  39. >>> import fibo
  40. This does not enter the names of the functions defined in ``fibo`` directly in
  41. the current symbol table; it only enters the module name ``fibo`` there. Using
  42. the module name you can access the functions::
  43. >>> fibo.fib(1000)
  44. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
  45. >>> fibo.fib2(100)
  46. [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  47. >>> fibo.__name__
  48. 'fibo'
  49. If you intend to use a function often you can assign it to a local name::
  50. >>> fib = fibo.fib
  51. >>> fib(500)
  52. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
  53. .. _tut-moremodules:
  54. More on Modules
  55. ===============
  56. A module can contain executable statements as well as function definitions.
  57. These statements are intended to initialize the module. They are executed only
  58. the *first* time the module name is encountered in an import statement. [#]_
  59. (They are also run if the file is executed as a script.)
  60. Each module has its own private symbol table, which is used as the global symbol
  61. table by all functions defined in the module. Thus, the author of a module can
  62. use global variables in the module without worrying about accidental clashes
  63. with a user's global variables. On the other hand, if you know what you are
  64. doing you can touch a module's global variables with the same notation used to
  65. refer to its functions, ``modname.itemname``.
  66. Modules can import other modules. It is customary but not required to place all
  67. :keyword:`import` statements at the beginning of a module (or script, for that
  68. matter). The imported module names are placed in the importing module's global
  69. symbol table.
  70. There is a variant of the :keyword:`import` statement that imports names from a
  71. module directly into the importing module's symbol table. For example::
  72. >>> from fibo import fib, fib2
  73. >>> fib(500)
  74. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
  75. This does not introduce the module name from which the imports are taken in the
  76. local symbol table (so in the example, ``fibo`` is not defined).
  77. There is even a variant to import all names that a module defines::
  78. >>> from fibo import *
  79. >>> fib(500)
  80. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
  81. This imports all names except those beginning with an underscore (``_``).
  82. In most cases Python programmers do not use this facility since it introduces
  83. an unknown set of names into the interpreter, possibly hiding some things
  84. you have already defined.
  85. Note that in general the practice of importing ``*`` from a module or package is
  86. frowned upon, since it often causes poorly readable code. However, it is okay to
  87. use it to save typing in interactive sessions.
  88. If the module name is followed by :keyword:`as`, then the name
  89. following :keyword:`as` is bound directly to the imported module.
  90. ::
  91. >>> import fibo as fib
  92. >>> fib.fib(500)
  93. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
  94. This is effectively importing the module in the same way that ``import fibo``
  95. will do, with the only difference of it being available as ``fib``.
  96. It can also be used when utilising :keyword:`from` with similar effects::
  97. >>> from fibo import fib as fibonacci
  98. >>> fibonacci(500)
  99. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
  100. .. note::
  101. For efficiency reasons, each module is only imported once per interpreter
  102. session. Therefore, if you change your modules, you must restart the
  103. interpreter -- or, if it's just one module you want to test interactively,
  104. use :func:`importlib.reload`, e.g. ``import importlib;
  105. importlib.reload(modulename)``.
  106. .. _tut-modulesasscripts:
  107. Executing modules as scripts
  108. ----------------------------
  109. When you run a Python module with ::
  110. python fibo.py <arguments>
  111. the code in the module will be executed, just as if you imported it, but with
  112. the ``__name__`` set to ``"__main__"``. That means that by adding this code at
  113. the end of your module::
  114. if __name__ == "__main__":
  115. import sys
  116. fib(int(sys.argv[1]))
  117. you can make the file usable as a script as well as an importable module,
  118. because the code that parses the command line only runs if the module is
  119. executed as the "main" file:
  120. .. code-block:: shell-session
  121. $ python fibo.py 50
  122. 0 1 1 2 3 5 8 13 21 34
  123. If the module is imported, the code is not run::
  124. >>> import fibo
  125. >>>
  126. This is often used either to provide a convenient user interface to a module, or
  127. for testing purposes (running the module as a script executes a test suite).
  128. .. _tut-searchpath:
  129. The Module Search Path
  130. ----------------------
  131. .. index:: triple: module; search; path
  132. When a module named :mod:`spam` is imported, the interpreter first searches for
  133. a built-in module with that name. If not found, it then searches for a file
  134. named :file:`spam.py` in a list of directories given by the variable
  135. :data:`sys.path`. :data:`sys.path` is initialized from these locations:
  136. * The directory containing the input script (or the current directory when no
  137. file is specified).
  138. * :envvar:`PYTHONPATH` (a list of directory names, with the same syntax as the
  139. shell variable :envvar:`PATH`).
  140. * The installation-dependent default.
  141. .. note::
  142. On file systems which support symlinks, the directory containing the input
  143. script is calculated after the symlink is followed. In other words the
  144. directory containing the symlink is **not** added to the module search path.
  145. After initialization, Python programs can modify :data:`sys.path`. The
  146. directory containing the script being run is placed at the beginning of the
  147. search path, ahead of the standard library path. This means that scripts in that
  148. directory will be loaded instead of modules of the same name in the library
  149. directory. This is an error unless the replacement is intended. See section
  150. :ref:`tut-standardmodules` for more information.
  151. .. %
  152. Do we need stuff on zip files etc. ? DUBOIS
  153. "Compiled" Python files
  154. -----------------------
  155. To speed up loading modules, Python caches the compiled version of each module
  156. in the ``__pycache__`` directory under the name :file:`module.{version}.pyc`,
  157. where the version encodes the format of the compiled file; it generally contains
  158. the Python version number. For example, in CPython release 3.3 the compiled
  159. version of spam.py would be cached as ``__pycache__/spam.cpython-33.pyc``. This
  160. naming convention allows compiled modules from different releases and different
  161. versions of Python to coexist.
  162. Python checks the modification date of the source against the compiled version
  163. to see if it's out of date and needs to be recompiled. This is a completely
  164. automatic process. Also, the compiled modules are platform-independent, so the
  165. same library can be shared among systems with different architectures.
  166. Python does not check the cache in two circumstances. First, it always
  167. recompiles and does not store the result for the module that's loaded directly
  168. from the command line. Second, it does not check the cache if there is no
  169. source module. To support a non-source (compiled only) distribution, the
  170. compiled module must be in the source directory, and there must not be a source
  171. module.
  172. Some tips for experts:
  173. * You can use the :option:`-O` or :option:`-OO` switches on the Python command
  174. to reduce the size of a compiled module. The ``-O`` switch removes assert
  175. statements, the ``-OO`` switch removes both assert statements and __doc__
  176. strings. Since some programs may rely on having these available, you should
  177. only use this option if you know what you're doing. "Optimized" modules have
  178. an ``opt-`` tag and are usually smaller. Future releases may
  179. change the effects of optimization.
  180. * A program doesn't run any faster when it is read from a ``.pyc``
  181. file than when it is read from a ``.py`` file; the only thing that's faster
  182. about ``.pyc`` files is the speed with which they are loaded.
  183. * The module :mod:`compileall` can create .pyc files for all modules in a
  184. directory.
  185. * There is more detail on this process, including a flow chart of the
  186. decisions, in :pep:`3147`.
  187. .. _tut-standardmodules:
  188. Standard Modules
  189. ================
  190. .. index:: module: sys
  191. Python comes with a library of standard modules, described in a separate
  192. document, the Python Library Reference ("Library Reference" hereafter). Some
  193. modules are built into the interpreter; these provide access to operations that
  194. are not part of the core of the language but are nevertheless built in, either
  195. for efficiency or to provide access to operating system primitives such as
  196. system calls. The set of such modules is a configuration option which also
  197. depends on the underlying platform. For example, the :mod:`winreg` module is only
  198. provided on Windows systems. One particular module deserves some attention:
  199. :mod:`sys`, which is built into every Python interpreter. The variables
  200. ``sys.ps1`` and ``sys.ps2`` define the strings used as primary and secondary
  201. prompts::
  202. >>> import sys
  203. >>> sys.ps1
  204. '>>> '
  205. >>> sys.ps2
  206. '... '
  207. >>> sys.ps1 = 'C> '
  208. C> print('Yuck!')
  209. Yuck!
  210. C>
  211. These two variables are only defined if the interpreter is in interactive mode.
  212. The variable ``sys.path`` is a list of strings that determines the interpreter's
  213. search path for modules. It is initialized to a default path taken from the
  214. environment variable :envvar:`PYTHONPATH`, or from a built-in default if
  215. :envvar:`PYTHONPATH` is not set. You can modify it using standard list
  216. operations::
  217. >>> import sys
  218. >>> sys.path.append('/ufs/guido/lib/python')
  219. .. _tut-dir:
  220. The :func:`dir` Function
  221. ========================
  222. The built-in function :func:`dir` is used to find out which names a module
  223. defines. It returns a sorted list of strings::
  224. >>> import fibo, sys
  225. >>> dir(fibo)
  226. ['__name__', 'fib', 'fib2']
  227. >>> dir(sys) # doctest: +NORMALIZE_WHITESPACE
  228. ['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__',
  229. '__package__', '__stderr__', '__stdin__', '__stdout__',
  230. '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe',
  231. '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv',
  232. 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder',
  233. 'call_tracing', 'callstats', 'copyright', 'displayhook',
  234. 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
  235. 'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
  236. 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
  237. 'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
  238. 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount',
  239. 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
  240. 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
  241. 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
  242. 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit',
  243. 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
  244. 'thread_info', 'version', 'version_info', 'warnoptions']
  245. Without arguments, :func:`dir` lists the names you have defined currently::
  246. >>> a = [1, 2, 3, 4, 5]
  247. >>> import fibo
  248. >>> fib = fibo.fib
  249. >>> dir()
  250. ['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']
  251. Note that it lists all types of names: variables, modules, functions, etc.
  252. .. index:: module: builtins
  253. :func:`dir` does not list the names of built-in functions and variables. If you
  254. want a list of those, they are defined in the standard module
  255. :mod:`builtins`::
  256. >>> import builtins
  257. >>> dir(builtins) # doctest: +NORMALIZE_WHITESPACE
  258. ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
  259. 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',
  260. 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
  261. 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
  262. 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
  263. 'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
  264. 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
  265. 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError',
  266. 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
  267. 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
  268. 'NotImplementedError', 'OSError', 'OverflowError',
  269. 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError',
  270. 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning',
  271. 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
  272. 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError',
  273. 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',
  274. 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
  275. 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__',
  276. '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs',
  277. 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable',
  278. 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',
  279. 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',
  280. 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',
  281. 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',
  282. 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview',
  283. 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
  284. 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
  285. 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars',
  286. 'zip']
  287. .. _tut-packages:
  288. Packages
  289. ========
  290. Packages are a way of structuring Python's module namespace by using "dotted
  291. module names". For example, the module name :mod:`A.B` designates a submodule
  292. named ``B`` in a package named ``A``. Just like the use of modules saves the
  293. authors of different modules from having to worry about each other's global
  294. variable names, the use of dotted module names saves the authors of multi-module
  295. packages like NumPy or Pillow from having to worry about
  296. each other's module names.
  297. Suppose you want to design a collection of modules (a "package") for the uniform
  298. handling of sound files and sound data. There are many different sound file
  299. formats (usually recognized by their extension, for example: :file:`.wav`,
  300. :file:`.aiff`, :file:`.au`), so you may need to create and maintain a growing
  301. collection of modules for the conversion between the various file formats.
  302. There are also many different operations you might want to perform on sound data
  303. (such as mixing, adding echo, applying an equalizer function, creating an
  304. artificial stereo effect), so in addition you will be writing a never-ending
  305. stream of modules to perform these operations. Here's a possible structure for
  306. your package (expressed in terms of a hierarchical filesystem):
  307. .. code-block:: text
  308. sound/ Top-level package
  309. __init__.py Initialize the sound package
  310. formats/ Subpackage for file format conversions
  311. __init__.py
  312. wavread.py
  313. wavwrite.py
  314. aiffread.py
  315. aiffwrite.py
  316. auread.py
  317. auwrite.py
  318. ...
  319. effects/ Subpackage for sound effects
  320. __init__.py
  321. echo.py
  322. surround.py
  323. reverse.py
  324. ...
  325. filters/ Subpackage for filters
  326. __init__.py
  327. equalizer.py
  328. vocoder.py
  329. karaoke.py
  330. ...
  331. When importing the package, Python searches through the directories on
  332. ``sys.path`` looking for the package subdirectory.
  333. The :file:`__init__.py` files are required to make Python treat the directories
  334. as containing packages; this is done to prevent directories with a common name,
  335. such as ``string``, from unintentionally hiding valid modules that occur later
  336. on the module search path. In the simplest case, :file:`__init__.py` can just be
  337. an empty file, but it can also execute initialization code for the package or
  338. set the ``__all__`` variable, described later.
  339. Users of the package can import individual modules from the package, for
  340. example::
  341. import sound.effects.echo
  342. This loads the submodule :mod:`sound.effects.echo`. It must be referenced with
  343. its full name. ::
  344. sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
  345. An alternative way of importing the submodule is::
  346. from sound.effects import echo
  347. This also loads the submodule :mod:`echo`, and makes it available without its
  348. package prefix, so it can be used as follows::
  349. echo.echofilter(input, output, delay=0.7, atten=4)
  350. Yet another variation is to import the desired function or variable directly::
  351. from sound.effects.echo import echofilter
  352. Again, this loads the submodule :mod:`echo`, but this makes its function
  353. :func:`echofilter` directly available::
  354. echofilter(input, output, delay=0.7, atten=4)
  355. Note that when using ``from package import item``, the item can be either a
  356. submodule (or subpackage) of the package, or some other name defined in the
  357. package, like a function, class or variable. The ``import`` statement first
  358. tests whether the item is defined in the package; if not, it assumes it is a
  359. module and attempts to load it. If it fails to find it, an :exc:`ImportError`
  360. exception is raised.
  361. Contrarily, when using syntax like ``import item.subitem.subsubitem``, each item
  362. except for the last must be a package; the last item can be a module or a
  363. package but can't be a class or function or variable defined in the previous
  364. item.
  365. .. _tut-pkg-import-star:
  366. Importing \* From a Package
  367. ---------------------------
  368. .. index:: single: __all__
  369. Now what happens when the user writes ``from sound.effects import *``? Ideally,
  370. one would hope that this somehow goes out to the filesystem, finds which
  371. submodules are present in the package, and imports them all. This could take a
  372. long time and importing sub-modules might have unwanted side-effects that should
  373. only happen when the sub-module is explicitly imported.
  374. The only solution is for the package author to provide an explicit index of the
  375. package. The :keyword:`import` statement uses the following convention: if a package's
  376. :file:`__init__.py` code defines a list named ``__all__``, it is taken to be the
  377. list of module names that should be imported when ``from package import *`` is
  378. encountered. It is up to the package author to keep this list up-to-date when a
  379. new version of the package is released. Package authors may also decide not to
  380. support it, if they don't see a use for importing \* from their package. For
  381. example, the file :file:`sound/effects/__init__.py` could contain the following
  382. code::
  383. __all__ = ["echo", "surround", "reverse"]
  384. This would mean that ``from sound.effects import *`` would import the three
  385. named submodules of the :mod:`sound` package.
  386. If ``__all__`` is not defined, the statement ``from sound.effects import *``
  387. does *not* import all submodules from the package :mod:`sound.effects` into the
  388. current namespace; it only ensures that the package :mod:`sound.effects` has
  389. been imported (possibly running any initialization code in :file:`__init__.py`)
  390. and then imports whatever names are defined in the package. This includes any
  391. names defined (and submodules explicitly loaded) by :file:`__init__.py`. It
  392. also includes any submodules of the package that were explicitly loaded by
  393. previous :keyword:`import` statements. Consider this code::
  394. import sound.effects.echo
  395. import sound.effects.surround
  396. from sound.effects import *
  397. In this example, the :mod:`echo` and :mod:`surround` modules are imported in the
  398. current namespace because they are defined in the :mod:`sound.effects` package
  399. when the ``from...import`` statement is executed. (This also works when
  400. ``__all__`` is defined.)
  401. Although certain modules are designed to export only names that follow certain
  402. patterns when you use ``import *``, it is still considered bad practice in
  403. production code.
  404. Remember, there is nothing wrong with using ``from Package import
  405. specific_submodule``! In fact, this is the recommended notation unless the
  406. importing module needs to use submodules with the same name from different
  407. packages.
  408. Intra-package References
  409. ------------------------
  410. When packages are structured into subpackages (as with the :mod:`sound` package
  411. in the example), you can use absolute imports to refer to submodules of siblings
  412. packages. For example, if the module :mod:`sound.filters.vocoder` needs to use
  413. the :mod:`echo` module in the :mod:`sound.effects` package, it can use ``from
  414. sound.effects import echo``.
  415. You can also write relative imports, with the ``from module import name`` form
  416. of import statement. These imports use leading dots to indicate the current and
  417. parent packages involved in the relative import. From the :mod:`surround`
  418. module for example, you might use::
  419. from . import echo
  420. from .. import formats
  421. from ..filters import equalizer
  422. Note that relative imports are based on the name of the current module. Since
  423. the name of the main module is always ``"__main__"``, modules intended for use
  424. as the main module of a Python application must always use absolute imports.
  425. Packages in Multiple Directories
  426. --------------------------------
  427. Packages support one more special attribute, :attr:`__path__`. This is
  428. initialized to be a list containing the name of the directory holding the
  429. package's :file:`__init__.py` before the code in that file is executed. This
  430. variable can be modified; doing so affects future searches for modules and
  431. subpackages contained in the package.
  432. While this feature is not often needed, it can be used to extend the set of
  433. modules found in a package.
  434. .. rubric:: Footnotes
  435. .. [#] In fact function definitions are also 'statements' that are 'executed'; the
  436. execution of a module-level function definition enters the function name in
  437. the module's global symbol table.