@@ -129,7 +129,7 @@ reference or call the method from a particular class. In C++, if you want to
129129use a method from a base class which is overridden in a derived class, you have
130130to use the ``:: `` operator -- in Python you can write
131131``baseclass.methodname(self, <argument list>) ``. This is particularly useful
132- for :meth: `__init__ ` methods, and in general in cases where a derived class
132+ for :meth: `~object. __init__ ` methods, and in general in cases where a derived class
133133method wants to extend the base class method of the same name and thus has to
134134call the base class method somehow.
135135
@@ -232,7 +232,8 @@ Similar methods exist for bytes and bytearray objects.
232232How fast are exceptions?
233233------------------------
234234
235- A try/except block is extremely efficient if no exceptions are raised. Actually
235+ A :keyword: `try `/:keyword: `except ` block is extremely efficient if no exceptions
236+ are raised. Actually
236237catching an exception is expensive. In versions of Python prior to 2.0 it was
237238common to use this idiom::
238239
@@ -352,7 +353,7 @@ will probably run out of file descriptors::
352353 c = f.read(1)
353354
354355Indeed, using CPython's reference counting and destructor scheme, each new
355- assignment to * f * closes the previous file. With a traditional GC, however,
356+ assignment to `` f `` closes the previous file. With a traditional GC, however,
356357those file objects will only get collected (and closed) at varying and possibly
357358long intervals.
358359
@@ -376,10 +377,10 @@ Python to work with it.)
376377
377378Traditional GC also becomes a problem when Python is embedded into other
378379applications. While in a standalone Python it's fine to replace the standard
379- malloc() and free() with versions provided by the GC library, an application
380- embedding Python may want to have its *own * substitute for malloc() and free(),
380+ `` malloc() `` and `` free() `` with versions provided by the GC library, an application
381+ embedding Python may want to have its *own * substitute for `` malloc() `` and `` free() `` ,
381382and may not want Python's. Right now, CPython works with anything that
382- implements malloc() and free() properly.
383+ implements `` malloc() `` and `` free() `` properly.
383384
384385
385386Why isn't all memory freed when CPython exits?
@@ -401,14 +402,15 @@ Why are there separate tuple and list data types?
401402
402403Lists and tuples, while similar in many respects, are generally used in
403404fundamentally different ways. Tuples can be thought of as being similar to
404- Pascal records or C structs; they're small collections of related data which may
405+ Pascal `` records `` or C `` structs `` ; they're small collections of related data which may
405406be of different types which are operated on as a group. For example, a
406407Cartesian coordinate is appropriately represented as a tuple of two or three
407408numbers.
408409
409410Lists, on the other hand, are more like arrays in other languages. They tend to
410411hold a varying number of objects all of which have the same type and which are
411- operated on one-by-one. For example, ``os.listdir('.') `` returns a list of
412+ operated on one-by-one. For example, :func: `os.listdir('.') <os.listdir> `
413+ returns a list of
412414strings representing the files in the current directory. Functions which
413415operate on this output would generally not break if you added another file or
414416two to the directory.
@@ -444,9 +446,9 @@ far) under most circumstances, and the implementation is simpler.
444446
445447Dictionaries work by computing a hash code for each key stored in the dictionary
446448using the :func: `hash ` built-in function. The hash code varies widely depending
447- on the key and a per-process seed; for example, " Python" could hash to
448- -539294296 while " python" , a string that differs by a single bit, could hash
449- to 1142331976. The hash code is then used to calculate a location in an
449+ on the key and a per-process seed; for example, `` ' Python' `` could hash to
450+ `` -539294296 `` while `` ' python' `` , a string that differs by a single bit, could hash
451+ to `` 1142331976 `` . The hash code is then used to calculate a location in an
450452internal array where the value will be stored. Assuming that you're storing
451453keys that all have different hash values, this means that dictionaries take
452454constant time -- O(1), in Big-O notation -- to retrieve a key.
@@ -497,7 +499,8 @@ Some unacceptable solutions that have been proposed:
497499
498500There is a trick to get around this if you need to, but use it at your own risk:
499501You can wrap a mutable structure inside a class instance which has both a
500- :meth: `__eq__ ` and a :meth: `__hash__ ` method. You must then make sure that the
502+ :meth: `~object.__eq__ ` and a :meth: `~object.__hash__ ` method.
503+ You must then make sure that the
501504hash value for all such wrapper objects that reside in a dictionary (or other
502505hash based structure), remain fixed while the object is in the dictionary (or
503506other structure). ::
@@ -528,7 +531,7 @@ is True``) then ``hash(o1) == hash(o2)`` (ie, ``o1.__hash__() == o2.__hash__()``
528531regardless of whether the object is in a dictionary or not. If you fail to meet
529532these restrictions dictionaries and other hash based structures will misbehave.
530533
531- In the case of ListWrapper, whenever the wrapper object is in a dictionary the
534+ In the case of :class: ` ! ListWrapper` , whenever the wrapper object is in a dictionary the
532535wrapped list must not change to avoid anomalies. Don't do this unless you are
533536prepared to think hard about the requirements and the consequences of not
534537meeting them correctly. Consider yourself warned.
@@ -581,9 +584,9 @@ exhaustive test suites that exercise every line of code in a module.
581584An appropriate testing discipline can help build large complex applications in
582585Python as well as having interface specifications would. In fact, it can be
583586better because an interface specification cannot test certain properties of a
584- program. For example, the :meth: `append ` method is expected to add new elements
587+ program. For example, the :meth: `list. append ` method is expected to add new elements
585588to the end of some internal list; an interface specification cannot test that
586- your :meth: `append ` implementation will actually do this correctly, but it's
589+ your :meth: `list. append ` implementation will actually do this correctly, but it's
587590trivial to check this property in a test suite.
588591
589592Writing test suites is very helpful, and you might want to design your code to
@@ -599,14 +602,14 @@ Why is there no goto?
599602In the 1970s people realized that unrestricted goto could lead
600603to messy "spaghetti" code that was hard to understand and revise.
601604In a high-level language, it is also unneeded as long as there
602- are ways to branch (in Python, with `` if `` statements and `` or ` `,
603- `` and `` , and `` if-else `` expressions) and loop (with `` while ` `
604- and `` for `` statements, possibly containing `` continue `` and `` break ` `).
605+ are ways to branch (in Python, with :keyword: ` if ` statements and :keyword: ` or `,
606+ :keyword: ` and `, and :keyword: ` if `/ :keyword: ` else ` expressions) and loop (with :keyword: ` while `
607+ and :keyword: ` for ` statements, possibly containing :keyword: ` continue ` and :keyword: ` break `).
605608
606609One can also use exceptions to provide a "structured goto"
607610that works even across
608611function calls. Many feel that exceptions can conveniently emulate all
609- reasonable uses of the "go" or " goto" constructs of C, Fortran, and other
612+ reasonable uses of the `` go `` or `` goto `` constructs of C, Fortran, and other
610613languages. For example::
611614
612615 class label(Exception): pass # declare a label
@@ -620,7 +623,7 @@ languages. For example::
620623 ...
621624
622625This doesn't allow you to jump into the middle of a loop, but that's usually
623- considered an abuse of goto anyway. Use sparingly.
626+ considered an abuse of `` goto `` anyway. Use sparingly.
624627
625628
626629Why can't raw strings (r-strings) end with a backslash?
@@ -652,7 +655,7 @@ If you're trying to build a pathname for a DOS command, try e.g. one of ::
652655Why doesn't Python have a "with" statement for attribute assignments?
653656---------------------------------------------------------------------
654657
655- Python has a ' with' statement that wraps the execution of a block, calling code
658+ Python has a :keyword: ` with ` statement that wraps the execution of a block, calling code
656659on the entrance and exit from the block. Some languages have a construct that
657660looks like this::
658661
@@ -679,13 +682,13 @@ For instance, take the following incomplete snippet::
679682 with a:
680683 print(x)
681684
682- The snippet assumes that "a" must have a member attribute called "x". However,
685+ The snippet assumes that `` a `` must have a member attribute called `` x ``. However,
683686there is nothing in Python that tells the interpreter this. What should happen
684- if "a" is, let us say, an integer? If there is a global variable named "x" ,
685- will it be used inside the with block? As you see, the dynamic nature of Python
687+ if `` a `` is, let us say, an integer? If there is a global variable named `` x `` ,
688+ will it be used inside the :keyword: ` with ` block? As you see, the dynamic nature of Python
686689makes such choices much harder.
687690
688- The primary benefit of " with" and similar language features (reduction of code
691+ The primary benefit of :keyword: ` with ` and similar language features (reduction of code
689692volume) can, however, easily be achieved in Python by assignment. Instead of::
690693
691694 function(args).mydict[index][index].a = 21
@@ -714,7 +717,8 @@ Why don't generators support the with statement?
714717For technical reasons, a generator used directly as a context manager
715718would not work correctly. When, as is most common, a generator is used as
716719an iterator run to completion, no closing is needed. When it is, wrap
717- it as "contextlib.closing(generator)" in the 'with' statement.
720+ it as :func: `contextlib.closing(generator) <contextlib.closing> `
721+ in the :keyword: `with ` statement.
718722
719723
720724Why are colons required for the if/while/def/class statements?
0 commit comments