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.

546 lines
23 KiB

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