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.

551 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
  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 b < n:
  27. print(b, 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 b < n:
  34. result.append(b)
  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. 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
  45. >>> fibo.fib2(100)
  46. [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. 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. 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. 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. .. note::
  89. For efficiency reasons, each module is only imported once per interpreter
  90. session. Therefore, if you change your modules, you must restart the
  91. interpreter -- or, if it's just one module you want to test interactively,
  92. use :func:`imp.reload`, e.g. ``import imp; imp.reload(modulename)``.
  93. .. _tut-modulesasscripts:
  94. Executing modules as scripts
  95. ----------------------------
  96. When you run a Python module with ::
  97. python fibo.py <arguments>
  98. the code in the module will be executed, just as if you imported it, but with
  99. the ``__name__`` set to ``"__main__"``. That means that by adding this code at
  100. the end of your module::
  101. if __name__ == "__main__":
  102. import sys
  103. fib(int(sys.argv[1]))
  104. you can make the file usable as a script as well as an importable module,
  105. because the code that parses the command line only runs if the module is
  106. executed as the "main" file::
  107. $ python fibo.py 50
  108. 1 1 2 3 5 8 13 21 34
  109. If the module is imported, the code is not run::
  110. >>> import fibo
  111. >>>
  112. This is often used either to provide a convenient user interface to a module, or
  113. for testing purposes (running the module as a script executes a test suite).
  114. .. _tut-searchpath:
  115. The Module Search Path
  116. ----------------------
  117. .. index:: triple: module; search; path
  118. When a module named :mod:`spam` is imported, the interpreter first searches for
  119. a built-in module with that name. If not found, it then searches for a file
  120. named :file:`spam.py` in a list of directories given by the variable
  121. :data:`sys.path`. :data:`sys.path` is initialized from these locations:
  122. * The directory containing the input script (or the current directory when no
  123. file is specified).
  124. * :envvar:`PYTHONPATH` (a list of directory names, with the same syntax as the
  125. shell variable :envvar:`PATH`).
  126. * The installation-dependent default.
  127. .. note::
  128. On file systems which support symlinks, the directory containing the input
  129. script is calculated after the symlink is followed. In other words the
  130. directory containing the symlink is **not** added to the module search path.
  131. After initialization, Python programs can modify :data:`sys.path`. The
  132. directory containing the script being run is placed at the beginning of the
  133. search path, ahead of the standard library path. This means that scripts in that
  134. directory will be loaded instead of modules of the same name in the library
  135. directory. This is an error unless the replacement is intended. See section
  136. :ref:`tut-standardmodules` for more information.
  137. .. %
  138. Do we need stuff on zip files etc. ? DUBOIS
  139. "Compiled" Python files
  140. -----------------------
  141. To speed up loading modules, Python caches the compiled version of each module
  142. in the ``__pycache__`` directory under the name :file:`module.{version}.pyc`,
  143. where the version encodes the format of the compiled file; it generally contains
  144. the Python version number. For example, in CPython release 3.3 the compiled
  145. version of spam.py would be cached as ``__pycache__/spam.cpython-33.pyc``. This
  146. naming convention allows compiled modules from different releases and different
  147. versions of Python to coexist.
  148. Python checks the modification date of the source against the compiled version
  149. to see if it's out of date and needs to be recompiled. This is a completely
  150. automatic process. Also, the compiled modules are platform-independent, so the
  151. same library can be shared among systems with different architectures.
  152. Python does not check the cache in two circumstances. First, it always
  153. recompiles and does not store the result for the module that's loaded directly
  154. from the command line. Second, it does not check the cache if there is no
  155. source module. To support a non-source (compiled only) distribution, the
  156. compiled module must be in the source directory, and there must not be a source
  157. module.
  158. Some tips for experts:
  159. * You can use the :option:`-O` or :option:`-OO` switches on the Python command
  160. to reduce the size of a compiled module. The ``-O`` switch removes assert
  161. statements, the ``-OO`` switch removes both assert statements and __doc__
  162. strings. Since some programs may rely on having these available, you should
  163. only use this option if you know what you're doing. "Optimized" modules have
  164. a .pyo rather than a .pyc suffix and are usually smaller. Future releases may
  165. change the effects of optimization.
  166. * A program doesn't run any faster when it is read from a ``.pyc`` or ``.pyo``
  167. file than when it is read from a ``.py`` file; the only thing that's faster
  168. about ``.pyc`` or ``.pyo`` files is the speed with which they are loaded.
  169. * The module :mod:`compileall` can create .pyc files (or .pyo files when
  170. :option:`-O` is used) for all modules in a directory.
  171. * There is more detail on this process, including a flow chart of the
  172. decisions, in PEP 3147.
  173. .. _tut-standardmodules:
  174. Standard Modules
  175. ================
  176. .. index:: module: sys
  177. Python comes with a library of standard modules, described in a separate
  178. document, the Python Library Reference ("Library Reference" hereafter). Some
  179. modules are built into the interpreter; these provide access to operations that
  180. are not part of the core of the language but are nevertheless built in, either
  181. for efficiency or to provide access to operating system primitives such as
  182. system calls. The set of such modules is a configuration option which also
  183. depends on the underlying platform. For example, the :mod:`winreg` module is only
  184. provided on Windows systems. One particular module deserves some attention:
  185. :mod:`sys`, which is built into every Python interpreter. The variables
  186. ``sys.ps1`` and ``sys.ps2`` define the strings used as primary and secondary
  187. prompts::
  188. >>> import sys
  189. >>> sys.ps1
  190. '>>> '
  191. >>> sys.ps2
  192. '... '
  193. >>> sys.ps1 = 'C> '
  194. C> print('Yuck!')
  195. Yuck!
  196. C>
  197. These two variables are only defined if the interpreter is in interactive mode.
  198. The variable ``sys.path`` is a list of strings that determines the interpreter's
  199. search path for modules. It is initialized to a default path taken from the
  200. environment variable :envvar:`PYTHONPATH`, or from a built-in default if
  201. :envvar:`PYTHONPATH` is not set. You can modify it using standard list
  202. operations::
  203. >>> import sys
  204. >>> sys.path.append('/ufs/guido/lib/python')
  205. .. _tut-dir:
  206. The :func:`dir` Function
  207. ========================
  208. The built-in function :func:`dir` is used to find out which names a module
  209. defines. It returns a sorted list of strings::
  210. >>> import fibo, sys
  211. >>> dir(fibo)
  212. ['__name__', 'fib', 'fib2']
  213. >>> dir(sys) # doctest: +NORMALIZE_WHITESPACE
  214. ['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__',
  215. '__package__', '__stderr__', '__stdin__', '__stdout__',
  216. '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe',
  217. '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv',
  218. 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder',
  219. 'call_tracing', 'callstats', 'copyright', 'displayhook',
  220. 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
  221. 'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
  222. 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
  223. 'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
  224. 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount',
  225. 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
  226. 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
  227. 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
  228. 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit',
  229. 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
  230. 'thread_info', 'version', 'version_info', 'warnoptions']
  231. Without arguments, :func:`dir` lists the names you have defined currently::
  232. >>> a = [1, 2, 3, 4, 5]
  233. >>> import fibo
  234. >>> fib = fibo.fib
  235. >>> dir()
  236. ['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']
  237. Note that it lists all types of names: variables, modules, functions, etc.
  238. .. index:: module: builtins
  239. :func:`dir` does not list the names of built-in functions and variables. If you
  240. want a list of those, they are defined in the standard module
  241. :mod:`builtins`::
  242. >>> import builtins
  243. >>> dir(builtins) # doctest: +NORMALIZE_WHITESPACE
  244. ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
  245. 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',
  246. 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
  247. 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
  248. 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
  249. 'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
  250. 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
  251. 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError',
  252. 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
  253. 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
  254. 'NotImplementedError', 'OSError', 'OverflowError',
  255. 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError',
  256. 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning',
  257. 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
  258. 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError',
  259. 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',
  260. 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
  261. 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__',
  262. '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs',
  263. 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable',
  264. 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',
  265. 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',
  266. 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',
  267. 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',
  268. 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview',
  269. 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
  270. 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
  271. 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars',
  272. 'zip']
  273. .. _tut-packages:
  274. Packages
  275. ========
  276. Packages are a way of structuring Python's module namespace by using "dotted
  277. module names". For example, the module name :mod:`A.B` designates a submodule
  278. named ``B`` in a package named ``A``. Just like the use of modules saves the
  279. authors of different modules from having to worry about each other's global
  280. variable names, the use of dotted module names saves the authors of multi-module
  281. packages like NumPy or the Python Imaging Library from having to worry about
  282. each other's module names.
  283. Suppose you want to design a collection of modules (a "package") for the uniform
  284. handling of sound files and sound data. There are many different sound file
  285. formats (usually recognized by their extension, for example: :file:`.wav`,
  286. :file:`.aiff`, :file:`.au`), so you may need to create and maintain a growing
  287. collection of modules for the conversion between the various file formats.
  288. There are also many different operations you might want to perform on sound data
  289. (such as mixing, adding echo, applying an equalizer function, creating an
  290. artificial stereo effect), so in addition you will be writing a never-ending
  291. stream of modules to perform these operations. Here's a possible structure for
  292. your package (expressed in terms of a hierarchical filesystem):
  293. .. code-block:: text
  294. sound/ Top-level package
  295. __init__.py Initialize the sound package
  296. formats/ Subpackage for file format conversions
  297. __init__.py
  298. wavread.py
  299. wavwrite.py
  300. aiffread.py
  301. aiffwrite.py
  302. auread.py
  303. auwrite.py
  304. ...
  305. effects/ Subpackage for sound effects
  306. __init__.py
  307. echo.py
  308. surround.py
  309. reverse.py
  310. ...
  311. filters/ Subpackage for filters
  312. __init__.py
  313. equalizer.py
  314. vocoder.py
  315. karaoke.py
  316. ...
  317. When importing the package, Python searches through the directories on
  318. ``sys.path`` looking for the package subdirectory.
  319. The :file:`__init__.py` files are required to make Python treat the directories
  320. as containing packages; this is done to prevent directories with a common name,
  321. such as ``string``, from unintentionally hiding valid modules that occur later
  322. on the module search path. In the simplest case, :file:`__init__.py` can just be
  323. an empty file, but it can also execute initialization code for the package or
  324. set the ``__all__`` variable, described later.
  325. Users of the package can import individual modules from the package, for
  326. example::
  327. import sound.effects.echo
  328. This loads the submodule :mod:`sound.effects.echo`. It must be referenced with
  329. its full name. ::
  330. sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
  331. An alternative way of importing the submodule is::
  332. from sound.effects import echo
  333. This also loads the submodule :mod:`echo`, and makes it available without its
  334. package prefix, so it can be used as follows::
  335. echo.echofilter(input, output, delay=0.7, atten=4)
  336. Yet another variation is to import the desired function or variable directly::
  337. from sound.effects.echo import echofilter
  338. Again, this loads the submodule :mod:`echo`, but this makes its function
  339. :func:`echofilter` directly available::
  340. echofilter(input, output, delay=0.7, atten=4)
  341. Note that when using ``from package import item``, the item can be either a
  342. submodule (or subpackage) of the package, or some other name defined in the
  343. package, like a function, class or variable. The ``import`` statement first
  344. tests whether the item is defined in the package; if not, it assumes it is a
  345. module and attempts to load it. If it fails to find it, an :exc:`ImportError`
  346. exception is raised.
  347. Contrarily, when using syntax like ``import item.subitem.subsubitem``, each item
  348. except for the last must be a package; the last item can be a module or a
  349. package but can't be a class or function or variable defined in the previous
  350. item.
  351. .. _tut-pkg-import-star:
  352. Importing \* From a Package
  353. ---------------------------
  354. .. index:: single: __all__
  355. Now what happens when the user writes ``from sound.effects import *``? Ideally,
  356. one would hope that this somehow goes out to the filesystem, finds which
  357. submodules are present in the package, and imports them all. This could take a
  358. long time and importing sub-modules might have unwanted side-effects that should
  359. only happen when the sub-module is explicitly imported.
  360. The only solution is for the package author to provide an explicit index of the
  361. package. The :keyword:`import` statement uses the following convention: if a package's
  362. :file:`__init__.py` code defines a list named ``__all__``, it is taken to be the
  363. list of module names that should be imported when ``from package import *`` is
  364. encountered. It is up to the package author to keep this list up-to-date when a
  365. new version of the package is released. Package authors may also decide not to
  366. support it, if they don't see a use for importing \* from their package. For
  367. example, the file :file:`sound/effects/__init__.py` could contain the following
  368. code::
  369. __all__ = ["echo", "surround", "reverse"]
  370. This would mean that ``from sound.effects import *`` would import the three
  371. named submodules of the :mod:`sound` package.
  372. If ``__all__`` is not defined, the statement ``from sound.effects import *``
  373. does *not* import all submodules from the package :mod:`sound.effects` into the
  374. current namespace; it only ensures that the package :mod:`sound.effects` has
  375. been imported (possibly running any initialization code in :file:`__init__.py`)
  376. and then imports whatever names are defined in the package. This includes any
  377. names defined (and submodules explicitly loaded) by :file:`__init__.py`. It
  378. also includes any submodules of the package that were explicitly loaded by
  379. previous :keyword:`import` statements. Consider this code::
  380. import sound.effects.echo
  381. import sound.effects.surround
  382. from sound.effects import *
  383. In this example, the :mod:`echo` and :mod:`surround` modules are imported in the
  384. current namespace because they are defined in the :mod:`sound.effects` package
  385. when the ``from...import`` statement is executed. (This also works when
  386. ``__all__`` is defined.)
  387. Although certain modules are designed to export only names that follow certain
  388. patterns when you use ``import *``, it is still considered bad practise in
  389. production code.
  390. Remember, there is nothing wrong with using ``from Package import
  391. specific_submodule``! In fact, this is the recommended notation unless the
  392. importing module needs to use submodules with the same name from different
  393. packages.
  394. Intra-package References
  395. ------------------------
  396. When packages are structured into subpackages (as with the :mod:`sound` package
  397. in the example), you can use absolute imports to refer to submodules of siblings
  398. packages. For example, if the module :mod:`sound.filters.vocoder` needs to use
  399. the :mod:`echo` module in the :mod:`sound.effects` package, it can use ``from
  400. sound.effects import echo``.
  401. You can also write relative imports, with the ``from module import name`` form
  402. of import statement. These imports use leading dots to indicate the current and
  403. parent packages involved in the relative import. From the :mod:`surround`
  404. module for example, you might use::
  405. from . import echo
  406. from .. import formats
  407. from ..filters import equalizer
  408. Note that relative imports are based on the name of the current module. Since
  409. the name of the main module is always ``"__main__"``, modules intended for use
  410. as the main module of a Python application must always use absolute imports.
  411. Packages in Multiple Directories
  412. --------------------------------
  413. Packages support one more special attribute, :attr:`__path__`. This is
  414. initialized to be a list containing the name of the directory holding the
  415. package's :file:`__init__.py` before the code in that file is executed. This
  416. variable can be modified; doing so affects future searches for modules and
  417. subpackages contained in the package.
  418. While this feature is not often needed, it can be used to extend the set of
  419. modules found in a package.
  420. .. rubric:: Footnotes
  421. .. [#] In fact function definitions are also 'statements' that are 'executed'; the
  422. execution of a module-level function definition enters the function name in
  423. the module's global symbol table.