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