Changeset 391 for python/trunk/Doc/faq/programming.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/faq/programming.rst
r2 r391 5 5 =============== 6 6 7 .. contents:: 7 .. only:: html 8 9 .. contents:: 8 10 9 11 General Questions … … 172 174 173 175 L2 = [] 174 for i in range [3]:176 for i in range(3): 175 177 L2.append(L1[i]) 176 178 177 179 it is much shorter and far faster to use :: 178 180 179 L2 = list(L1[:3]) # "list" is redundant if L1 is a list.181 L2 = list(L1[:3]) # "list" is redundant if L1 is a list. 180 182 181 183 Note that the functionally-oriented built-in functions such as :func:`map`, … … 184 186 together:: 185 187 186 >>> zip([1, 2,3], [4,5,6])188 >>> zip([1, 2, 3], [4, 5, 6]) 187 189 [(1, 4), (2, 5), (3, 6)] 188 190 189 191 or to compute a number of sines:: 190 192 191 >>> map( math.sin, (1,2,3,4))192 [0.841470984808, 0.909297426826, 0.14112000806, 193 >>> map(math.sin, (1, 2, 3, 4)) 194 [0.841470984808, 0.909297426826, 0.14112000806, -0.756802495308] 193 195 194 196 The operation completes very quickly in such cases. 195 197 196 Other examples include the ``join()`` and ``split()`` methods of string objects. 198 Other examples include the ``join()`` and ``split()`` :ref:`methods 199 of string objects <string-methods>`. 197 200 For example if s1..s7 are large (10K+) strings then 198 201 ``"".join([s1,s2,s3,s4,s5,s6,s7])`` may be far faster than the more obvious 199 202 ``s1+s2+s3+s4+s5+s6+s7``, since the "summation" will compute many 200 203 subexpressions, whereas ``join()`` does all the copying in one pass. For 201 manipulating strings, use the ``replace()`` method on string objects. Use202 regular expressions only when you're not dealing with constant string patterns. 203 Consider using the string formatting operations ``string % tuple`` and ``string 204 % dictionary``.204 manipulating strings, use the ``replace()`` and the ``format()`` :ref:`methods 205 on string objects <string-methods>`. Use regular expressions only when you're 206 not dealing with constant string patterns. You may still use :ref:`the old % 207 operations <string-formatting>` ``string % tuple`` and ``string % dictionary``. 205 208 206 209 Be sure to use the :meth:`list.sort` built-in method to do sorting, and see the … … 212 215 suppose you have a program that runs slowly and you use the profiler to 213 216 determine that a Python function ``ff()`` is being called lots of times. If you 214 notice that ``ff 217 notice that ``ff()``:: 215 218 216 219 def ff(x): … … 333 336 11 334 337 335 In Python3, you can do a similar thing in a nested scope using the336 :keyword:`nonlocal` keyword:337 338 .. doctest::339 :options: +SKIP340 341 >>> def foo():342 ... x = 10343 ... def bar():344 ... nonlocal x345 ... print x346 ... x += 1347 ... bar()348 ... print x349 >>> foo()350 10351 11352 353 338 354 339 What are the rules for local and global variables in Python? … … 370 355 371 356 357 Why do lambdas defined in a loop with different values all return the same result? 358 ---------------------------------------------------------------------------------- 359 360 Assume you use a for loop to define a few different lambdas (or even plain 361 functions), e.g.:: 362 363 >>> squares = [] 364 >>> for x in range(5): 365 ... squares.append(lambda: x**2) 366 367 This gives you a list that contains 5 lambdas that calculate ``x**2``. You 368 might expect that, when called, they would return, respectively, ``0``, ``1``, 369 ``4``, ``9``, and ``16``. However, when you actually try you will see that 370 they all return ``16``:: 371 372 >>> squares[2]() 373 16 374 >>> squares[4]() 375 16 376 377 This happens because ``x`` is not local to the lambdas, but is defined in 378 the outer scope, and it is accessed when the lambda is called --- not when it 379 is defined. At the end of the loop, the value of ``x`` is ``4``, so all the 380 functions now return ``4**2``, i.e. ``16``. You can also verify this by 381 changing the value of ``x`` and see how the results of the lambdas change:: 382 383 >>> x = 8 384 >>> squares[2]() 385 64 386 387 In order to avoid this, you need to save the values in variables local to the 388 lambdas, so that they don't rely on the value of the global ``x``:: 389 390 >>> squares = [] 391 >>> for x in range(5): 392 ... squares.append(lambda n=x: n**2) 393 394 Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed 395 when the lambda is defined so that it has the same value that ``x`` had at 396 that point in the loop. This means that the value of ``n`` will be ``0`` 397 in the first lambda, ``1`` in the second, ``2`` in the third, and so on. 398 Therefore each lambda will now return the correct result:: 399 400 >>> squares[2]() 401 4 402 >>> squares[4]() 403 16 404 405 Note that this behaviour is not peculiar to lambdas, but applies to regular 406 functions too. 407 408 372 409 How do I share global variables across modules? 373 410 ------------------------------------------------ … … 413 450 It's good practice if you import modules in the following order: 414 451 415 1. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re`` )452 1. standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re`` 416 453 2. third-party library modules (anything installed in Python's site-packages 417 454 directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc. … … 422 459 write ``import m2``, even though it's legal. Write ``from package.sub import 423 460 m2`` instead. Relative imports can lead to a module being initialized twice, 424 leading to confusing bugs. 461 leading to confusing bugs. See :pep:`328` for details. 425 462 426 463 It is sometimes necessary to move imports to a function or class to avoid … … 487 524 488 525 526 .. index:: 527 single: argument; difference from parameter 528 single: parameter; difference from argument 529 530 .. _faq-argument-vs-parameter: 531 532 What is the difference between arguments and parameters? 533 -------------------------------------------------------- 534 535 :term:`Parameters <parameter>` are defined by the names that appear in a 536 function definition, whereas :term:`arguments <argument>` are the values 537 actually passed to a function when calling it. Parameters define what types of 538 arguments a function can accept. For example, given the function definition:: 539 540 def func(foo, bar=None, **kwargs): 541 pass 542 543 *foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling 544 ``func``, for example:: 545 546 func(42, bar=314, extra=somevar) 547 548 the values ``42``, ``314``, and ``somevar`` are arguments. 549 550 489 551 How do I write a function with output parameters (call by reference)? 490 552 --------------------------------------------------------------------- … … 650 712 b = a 651 713 print b 652 <__main__.A instance at 0 16D07CC>714 <__main__.A instance at 0x16D07CC> 653 715 print a 654 <__main__.A instance at 0 16D07CC>716 <__main__.A instance at 0x16D07CC> 655 717 656 718 Arguably the class has a name: even though it is bound to two names and invoked … … 682 744 683 745 >>> "a" in "b", "a" 684 (False, ' 1')746 (False, 'a') 685 747 686 748 Since the comma is not an operator, but a separator between expressions the 687 749 above is evaluated as if you had entered:: 688 750 689 >>>("a" in "b"), "a"751 ("a" in "b"), "a" 690 752 691 753 not:: 692 754 693 >>> "a" in ("5", "a")755 "a" in ("b", "a") 694 756 695 757 The same is true of the various assignment operators (``=``, ``+=`` etc). They … … 709 771 710 772 For versions previous to 2.5 the answer would be 'No'. 711 712 .. XXX remove rest?713 714 In many cases you can mimic ``a ? b : c`` with ``a and b or c``, but there's a715 flaw: if *b* is zero (or empty, or ``None`` -- anything that tests false) then716 *c* will be selected instead. In many cases you can prove by looking at the717 code that this can't happen (e.g. because *b* is a constant or has a type that718 can never be false), but in general this can be a problem.719 720 Tim Peters (who wishes it was Steve Majewski) suggested the following solution:721 ``(a and [b] or [c])[0]``. Because ``[b]`` is a singleton list it is never722 false, so the wrong path is never taken; then applying ``[0]`` to the whole723 thing gets the *b* or *c* that you really wanted. Ugly, but it gets you there724 in the rare cases where it is really inconvenient to rewrite your code using725 'if'.726 727 The best course is usually to write a simple ``if...else`` statement. Another728 solution is to implement the ``?:`` operator as a function::729 730 def q(cond, on_true, on_false):731 if cond:732 if not isfunction(on_true):733 return on_true734 else:735 return apply(on_true)736 else:737 if not isfunction(on_false):738 return on_false739 else:740 return apply(on_false)741 742 In most cases you'll pass b and c directly: ``q(a, b, c)``. To avoid evaluating743 b or c when they shouldn't be, encapsulate them within a lambda function, e.g.:744 ``q(a, lambda: b, lambda: c)``.745 746 It has been asked *why* Python has no if-then-else expression. There are747 several answers: many languages do just fine without one; it can easily lead to748 less readable code; no sufficiently "Pythonic" syntax has been discovered; a749 search of the standard library found remarkably few places where using an750 if-then-else expression would make the code more understandable.751 752 In 2002, :pep:`308` was written proposing several possible syntaxes and the753 community was asked to vote on the issue. The vote was inconclusive. Most754 people liked one of the syntaxes, but also hated other syntaxes; many votes755 implied that people preferred no ternary operator rather than having a syntax756 they hated.757 773 758 774 … … 768 784 769 785 # First 10 Fibonacci numbers 770 print map(lambda x,f=lambda x,f:( x<=1) or (f(x-1,f)+f(x-2,f)): f(x,f),786 print map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1: f(x,f), 771 787 range(10)) 772 788 … … 794 810 ------------------------------------------------ 795 811 796 To specify an octal digit, precede the octal value with a zero. For example, to 797 set the variable "a" to the octal value "10" (8 in decimal), type:: 798 799 >>> a = 010 812 To specify an octal digit, precede the octal value with a zero, and then a lower 813 or uppercase "o". For example, to set the variable "a" to the octal value "10" 814 (8 in decimal), type:: 815 816 >>> a = 0o10 800 817 >>> a 801 818 8 … … 813 830 814 831 815 Why does -22 / 10 return -3?816 ---------------------------- 832 Why does -22 // 10 return -3? 833 ----------------------------- 817 834 818 835 It's primarily driven by the desire that ``i % j`` have the same sign as ``j``. 819 836 If you want that, and also want:: 820 837 821 i == (i / j) * j + (i % j)838 i == (i // j) * j + (i % j) 822 839 823 840 then integer division has to return the floor. C also requires that identity to 824 hold, and then compilers that truncate ``i / j`` need to make ``i % j`` have the825 same sign as ``i``.841 hold, and then compilers that truncate ``i // j`` need to make ``i % j`` have 842 the same sign as ``i``. 826 843 827 844 There are few real use cases for ``i % j`` when ``j`` is negative. When ``j`` … … 830 847 ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a bug waiting to 831 848 bite. 849 850 .. note:: 851 852 On Python 2, ``a / b`` returns the same as ``a // b`` if 853 ``__future__.division`` is not in effect. This is also known as "classic" 854 division. 832 855 833 856 … … 862 885 To convert, e.g., the number 144 to the string '144', use the built-in type 863 886 constructor :func:`str`. If you want a hexadecimal or octal representation, use 864 the built-in functions ``hex()`` or ``oct()``. For fancy formatting, use 865 :ref:`the % operator <string-formatting>` on strings, e.g. ``"%04d" % 144`` 866 yields ``'0144'`` and ``"%.3f" % (1/3.0)`` yields ``'0.333'``. See the library 867 reference manual for details. 887 the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see 888 the :ref:`formatstrings` section, e.g. ``"{:04d}".format(144)`` yields 889 ``'0144'`` and ``"{:.3f}".format(1/3)`` yields ``'0.333'``. You may also use 890 :ref:`the % operator <string-formatting>` on strings. See the library reference 891 manual for details. 868 892 869 893 … … 874 898 ability, try converting the string to a list or use the array module:: 875 899 900 >>> import io 876 901 >>> s = "Hello, world" 877 902 >>> a = list(s) … … 887 912 array('c', 'Hello, world') 888 913 >>> a[0] = 'y' ; print a 889 array('c', 'yello world')914 array('c', 'yello, world') 890 915 >>> a.tostring() 891 916 'yello, world' … … 954 979 955 980 Starting with Python 2.2, you can use ``S.rstrip("\r\n")`` to remove all 956 occur ences of any line terminator from the end of the string ``S`` without981 occurrences of any line terminator from the end of the string ``S`` without 957 982 removing other trailing whitespace. If the string ``S`` represents more than 958 983 one line, with several empty lines at the end, the line terminators for all the … … 963 988 ... "\r\n") 964 989 >>> lines.rstrip("\n\r") 965 "line 1 "990 'line 1 ' 966 991 967 992 Since this is typically only desired when reading text one line at a time, using 968 993 ``S.rstrip()`` this way works well. 969 994 970 For older versions of Python, There are two partial substitutes:995 For older versions of Python, there are two partial substitutes: 971 996 972 997 - If you want to remove all trailing whitespace, use the ``rstrip()`` method of … … 989 1014 if the line uses something other than whitespace as a separator. 990 1015 991 For more complicated input parsing, regular expressions more powerful than C's992 :cfunc:`sscanf` and better suited for the task.1016 For more complicated input parsing, regular expressions are more powerful 1017 than C's :c:func:`sscanf` and better suited for the task. 993 1018 994 1019 … … 1094 1119 list, deleting duplicates as you go:: 1095 1120 1096 if List:1097 List.sort()1098 last = List[-1]1099 for i in range(len( List)-2, -1, -1):1100 if last == List[i]:1101 del List[i]1121 if mylist: 1122 mylist.sort() 1123 last = mylist[-1] 1124 for i in range(len(mylist)-2, -1, -1): 1125 if last == mylist[i]: 1126 del mylist[i] 1102 1127 else: 1103 last = List[i]1128 last = mylist[i] 1104 1129 1105 1130 If all elements of the list may be used as dictionary keys (i.e. they are all … … 1107 1132 1108 1133 d = {} 1109 for x in List:1110 d[x] = x1111 List = d.values()1134 for x in mylist: 1135 d[x] = 1 1136 mylist = list(d.keys()) 1112 1137 1113 1138 In Python 2.5 and later, the following is possible instead:: 1114 1139 1115 List = list(set(List))1140 mylist = list(set(mylist)) 1116 1141 1117 1142 This converts the list into a set, thereby removing duplicates, and then back … … 1149 1174 You probably tried to make a multidimensional array like this:: 1150 1175 1151 A = [[None] * 2] * 31176 >>> A = [[None] * 2] * 3 1152 1177 1153 1178 This looks correct if you print it:: … … 1181 1206 1182 1207 Or, you can use an extension that provides a matrix datatype; `Numeric Python 1183 <http:// numpy.scipy.org/>`_ is the best known.1208 <http://www.numpy.org/>`_ is the best known. 1184 1209 1185 1210 … … 1189 1214 Use a list comprehension:: 1190 1215 1191 result = [obj.method() for obj in List]1216 result = [obj.method() for obj in mylist] 1192 1217 1193 1218 More generically, you can try the following function:: … … 1200 1225 1201 1226 1227 Why does a_tuple[i] += ['item'] raise an exception when the addition works? 1228 --------------------------------------------------------------------------- 1229 1230 This is because of a combination of the fact that augmented assignment 1231 operators are *assignment* operators, and the difference between mutable and 1232 immutable objects in Python. 1233 1234 This discussion applies in general when augmented assignment operators are 1235 applied to elements of a tuple that point to mutable objects, but we'll use 1236 a ``list`` and ``+=`` as our exemplar. 1237 1238 If you wrote:: 1239 1240 >>> a_tuple = (1, 2) 1241 >>> a_tuple[0] += 1 1242 Traceback (most recent call last): 1243 ... 1244 TypeError: 'tuple' object does not support item assignment 1245 1246 The reason for the exception should be immediately clear: ``1`` is added to the 1247 object ``a_tuple[0]`` points to (``1``), producing the result object, ``2``, 1248 but when we attempt to assign the result of the computation, ``2``, to element 1249 ``0`` of the tuple, we get an error because we can't change what an element of 1250 a tuple points to. 1251 1252 Under the covers, what this augmented assignment statement is doing is 1253 approximately this:: 1254 1255 >>> result = a_tuple[0] + 1 1256 >>> a_tuple[0] = result 1257 Traceback (most recent call last): 1258 ... 1259 TypeError: 'tuple' object does not support item assignment 1260 1261 It is the assignment part of the operation that produces the error, since a 1262 tuple is immutable. 1263 1264 When you write something like:: 1265 1266 >>> a_tuple = (['foo'], 'bar') 1267 >>> a_tuple[0] += ['item'] 1268 Traceback (most recent call last): 1269 ... 1270 TypeError: 'tuple' object does not support item assignment 1271 1272 The exception is a bit more surprising, and even more surprising is the fact 1273 that even though there was an error, the append worked:: 1274 1275 >>> a_tuple[0] 1276 ['foo', 'item'] 1277 1278 To see why this happens, you need to know that (a) if an object implements an 1279 ``__iadd__`` magic method, it gets called when the ``+=`` augmented assignment 1280 is executed, and its return value is what gets used in the assignment statement; 1281 and (b) for lists, ``__iadd__`` is equivalent to calling ``extend`` on the list 1282 and returning the list. That's why we say that for lists, ``+=`` is a 1283 "shorthand" for ``list.extend``:: 1284 1285 >>> a_list = [] 1286 >>> a_list += [1] 1287 >>> a_list 1288 [1] 1289 1290 This is equivalent to:: 1291 1292 >>> result = a_list.__iadd__([1]) 1293 >>> a_list = result 1294 1295 The object pointed to by a_list has been mutated, and the pointer to the 1296 mutated object is assigned back to ``a_list``. The end result of the 1297 assignment is a no-op, since it is a pointer to the same object that ``a_list`` 1298 was previously pointing to, but the assignment still happens. 1299 1300 Thus, in our tuple example what is happening is equivalent to:: 1301 1302 >>> result = a_tuple[0].__iadd__(['item']) 1303 >>> a_tuple[0] = result 1304 Traceback (most recent call last): 1305 ... 1306 TypeError: 'tuple' object does not support item assignment 1307 1308 The ``__iadd__`` succeeds, and thus the list is extended, but even though 1309 ``result`` points to the same object that ``a_tuple[0]`` already points to, 1310 that final assignment still results in an error, because tuples are immutable. 1311 1312 1202 1313 Dictionaries 1203 1314 ============ … … 1214 1325 be presented in order sorted by the key. 1215 1326 1216 A more complicated solution is to subclass `` UserDict.UserDict`` to create a1327 A more complicated solution is to subclass ``dict`` to create a 1217 1328 ``SortedDict`` class that prints itself in a predictable order. Here's one 1218 1329 simpleminded implementation of such a class:: 1219 1330 1220 import UserDict, string 1221 1222 class SortedDict(UserDict.UserDict): 1331 class SortedDict(dict): 1223 1332 def __repr__(self): 1224 result = [] 1225 append = result.append 1226 keys = self.data.keys() 1227 keys.sort() 1228 for k in keys: 1229 append("%s: %s" % (`k`, `self.data[k]`)) 1230 return "{%s}" % string.join(result, ", ") 1231 1232 __str__ = __repr__ 1333 keys = sorted(self.keys()) 1334 result = ("{!r}: {!r}".format(k, self[k]) for k in keys) 1335 return "{{{}}}".format(", ".join(result)) 1336 1337 __str__ = __repr__ 1233 1338 1234 1339 This will work for many common situations you might encounter, though it's far … … 1252 1357 strings by their uppercase values:: 1253 1358 1254 tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform1359 tmp1 = [(x.upper(), x) for x in L] # Schwartzian transform 1255 1360 tmp1.sort() 1256 1361 Usorted = [x[1] for x in tmp1] … … 1259 1364 each string:: 1260 1365 1261 tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform1366 tmp2 = [(int(s[10:15]), s) for s in L] # Schwartzian transform 1262 1367 tmp2.sort() 1263 1368 Isorted = [x[1] for x in tmp2] … … 1296 1401 An alternative for the last step is:: 1297 1402 1298 result = []1299 for p in pairs: result.append(p[1])1403 >>> result = [] 1404 >>> for p in pairs: result.append(p[1]) 1300 1405 1301 1406 If you find this more legible, you might prefer to use this instead of the final … … 1365 1470 that does something:: 1366 1471 1367 def search 1472 def search(obj): 1368 1473 if isinstance(obj, Mailbox): 1369 1474 # ... code to search a mailbox … … 1468 1573 ----------------------------------------------------------- 1469 1574 1470 Static data (in the sense of C++ or Java) is easy; static methods (again in the 1471 sense of C++ or Java) are not supported directly.1575 Both static data and static methods (in the sense of C++ or Java) are supported 1576 in Python. 1472 1577 1473 1578 For static data, simply define a class attribute. To assign a new value to the … … 1488 1593 1489 1594 Caution: within a method of C, an assignment like ``self.count = 42`` creates a 1490 new and unrelated instance vrbl named "count" in ``self``'s own dict. Rebinding1491 of a class-static data name must always specify the class whether inside a 1492 method ornot::1595 new and unrelated instance named "count" in ``self``'s own dict. Rebinding of a 1596 class-static data name must always specify the class whether inside a method or 1597 not:: 1493 1598 1494 1599 C.count = 314 … … 1620 1725 1621 1726 1727 Why does the result of ``id()`` appear to be not unique? 1728 -------------------------------------------------------- 1729 1730 The :func:`id` builtin returns an integer that is guaranteed to be unique during 1731 the lifetime of the object. Since in CPython, this is the object's memory 1732 address, it happens frequently that after an object is deleted from memory, the 1733 next freshly created object is allocated at the same position in memory. This 1734 is illustrated by this example: 1735 1736 >>> id(1000) 1737 13901272 1738 >>> id(2000) 1739 13901272 1740 1741 The two ids belong to different integer objects that are created before, and 1742 deleted immediately after execution of the ``id()`` call. To be sure that 1743 objects whose id you want to examine are still alive, create another reference 1744 to the object: 1745 1746 >>> a = 1000; b = 2000 1747 >>> id(a) 1748 13901272 1749 >>> id(b) 1750 13891296 1751 1752 1622 1753 Modules 1623 1754 ======= … … 1637 1768 directory. 1638 1769 1639 Running Python on a top level script is not considered an import and no ``.pyc``1640 will be created. For example, if you have a top-level module ``abc.py`` that 1641 imports another module ``xyz.py``, when you run abc, ``xyz.pyc`` will be created 1642 since xyz is imported, but no ``abc.pyc`` file will be created since ``abc.py`` 1643 isn't being imported.1644 1645 If you need to create abc.pyc -- that is, to create a .pycfile for a module1770 Running Python on a top level script is not considered an import and no 1771 ``.pyc`` will be created. For example, if you have a top-level module 1772 ``foo.py`` that imports another module ``xyz.py``, when you run ``foo``, 1773 ``xyz.pyc`` will be created since ``xyz`` is imported, but no ``foo.pyc`` file 1774 will be created since ``foo.py`` isn't being imported. 1775 1776 If you need to create ``foo.pyc`` -- that is, to create a ``.pyc`` file for a module 1646 1777 that is not imported -- you can, using the :mod:`py_compile` and 1647 1778 :mod:`compileall` modules. … … 1651 1782 1652 1783 >>> import py_compile 1653 >>> py_compile.compile(' abc.py')1654 1655 This will write the ``.pyc`` to the same location as `` abc.py`` (or you can1784 >>> py_compile.compile('foo.py') # doctest: +SKIP 1785 1786 This will write the ``.pyc`` to the same location as ``foo.py`` (or you can 1656 1787 override that with the optional parameter ``cfile``). 1657 1788
Note:
See TracChangeset
for help on using the changeset viewer.