@@ -129,7 +129,7 @@ reference or call the method from a particular class. In C++, if you want to
129
129
use a method from a base class which is overridden in a derived class, you have
130
130
to use the ``:: `` operator -- in Python you can write
131
131
``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
133
133
method wants to extend the base class method of the same name and thus has to
134
134
call the base class method somehow.
135
135
@@ -232,7 +232,8 @@ Similar methods exist for bytes and bytearray objects.
232
232
How fast are exceptions?
233
233
------------------------
234
234
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
236
237
catching an exception is expensive. In versions of Python prior to 2.0 it was
237
238
common to use this idiom::
238
239
@@ -352,7 +353,7 @@ will probably run out of file descriptors::
352
353
c = f.read(1)
353
354
354
355
Indeed, 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,
356
357
those file objects will only get collected (and closed) at varying and possibly
357
358
long intervals.
358
359
@@ -376,10 +377,10 @@ Python to work with it.)
376
377
377
378
Traditional GC also becomes a problem when Python is embedded into other
378
379
applications. 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() `` ,
381
382
and may not want Python's. Right now, CPython works with anything that
382
- implements malloc() and free() properly.
383
+ implements `` malloc() `` and `` free() `` properly.
383
384
384
385
385
386
Why isn't all memory freed when CPython exits?
@@ -401,14 +402,15 @@ Why are there separate tuple and list data types?
401
402
402
403
Lists and tuples, while similar in many respects, are generally used in
403
404
fundamentally 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
405
406
be of different types which are operated on as a group. For example, a
406
407
Cartesian coordinate is appropriately represented as a tuple of two or three
407
408
numbers.
408
409
409
410
Lists, on the other hand, are more like arrays in other languages. They tend to
410
411
hold 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
412
414
strings representing the files in the current directory. Functions which
413
415
operate on this output would generally not break if you added another file or
414
416
two to the directory.
@@ -444,9 +446,9 @@ far) under most circumstances, and the implementation is simpler.
444
446
445
447
Dictionaries work by computing a hash code for each key stored in the dictionary
446
448
using 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
450
452
internal array where the value will be stored. Assuming that you're storing
451
453
keys that all have different hash values, this means that dictionaries take
452
454
constant time -- O(1), in Big-O notation -- to retrieve a key.
@@ -497,7 +499,8 @@ Some unacceptable solutions that have been proposed:
497
499
498
500
There is a trick to get around this if you need to, but use it at your own risk:
499
501
You 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
501
504
hash value for all such wrapper objects that reside in a dictionary (or other
502
505
hash based structure), remain fixed while the object is in the dictionary (or
503
506
other structure). ::
@@ -528,7 +531,7 @@ is True``) then ``hash(o1) == hash(o2)`` (ie, ``o1.__hash__() == o2.__hash__()``
528
531
regardless of whether the object is in a dictionary or not. If you fail to meet
529
532
these restrictions dictionaries and other hash based structures will misbehave.
530
533
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
532
535
wrapped list must not change to avoid anomalies. Don't do this unless you are
533
536
prepared to think hard about the requirements and the consequences of not
534
537
meeting them correctly. Consider yourself warned.
@@ -581,9 +584,9 @@ exhaustive test suites that exercise every line of code in a module.
581
584
An appropriate testing discipline can help build large complex applications in
582
585
Python as well as having interface specifications would. In fact, it can be
583
586
better 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
585
588
to 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
587
590
trivial to check this property in a test suite.
588
591
589
592
Writing test suites is very helpful, and you might want to design your code to
@@ -599,14 +602,14 @@ Why is there no goto?
599
602
In the 1970s people realized that unrestricted goto could lead
600
603
to messy "spaghetti" code that was hard to understand and revise.
601
604
In 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 `).
605
608
606
609
One can also use exceptions to provide a "structured goto"
607
610
that works even across
608
611
function 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
610
613
languages. For example::
611
614
612
615
class label(Exception): pass # declare a label
@@ -620,7 +623,7 @@ languages. For example::
620
623
...
621
624
622
625
This 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.
624
627
625
628
626
629
Why 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 ::
652
655
Why doesn't Python have a "with" statement for attribute assignments?
653
656
---------------------------------------------------------------------
654
657
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
656
659
on the entrance and exit from the block. Some languages have a construct that
657
660
looks like this::
658
661
@@ -679,13 +682,13 @@ For instance, take the following incomplete snippet::
679
682
with a:
680
683
print(x)
681
684
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,
683
686
there 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
686
689
makes such choices much harder.
687
690
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
689
692
volume) can, however, easily be achieved in Python by assignment. Instead of::
690
693
691
694
function(args).mydict[index][index].a = 21
@@ -714,7 +717,8 @@ Why don't generators support the with statement?
714
717
For technical reasons, a generator used directly as a context manager
715
718
would not work correctly. When, as is most common, a generator is used as
716
719
an 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.
718
722
719
723
720
724
Why are colons required for the if/while/def/class statements?
0 commit comments