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.

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