Skip to content

Commit 53bc2db

Browse files
slatenyezio-melottiCAM-Gerlach
authored andcommitted
pythongh-96265: Fix some formatting in faq/design.rst (python#96924)
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com> Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
1 parent 0085f20 commit 53bc2db

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

Doc/faq/design.rst

+31-27
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ and think it is a bug in Python. It's not. This has little to do with Python,
6262
and much more to do with how the underlying platform handles floating-point
6363
numbers.
6464

65-
The :class:`float` type in CPython uses a C ``double`` for storage. A
65+
The :class:`float` type in CPython uses a C :c:type:`double` for storage. A
6666
:class:`float` object's value is stored in binary floating-point with a fixed
6767
precision (typically 53 bits) and Python uses C operations, which in turn rely
6868
on the hardware implementation in the processor, to perform floating-point
@@ -129,7 +129,7 @@ reference or call the method from a particular class. In C++, if you want to
129129
use a method from a base class which is overridden in a derived class, you have
130130
to 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
133133
method wants to extend the base class method of the same name and thus has to
134134
call the base class method somehow.
135135

@@ -232,7 +232,8 @@ Similar methods exist for bytes and bytearray objects.
232232
How 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
236237
catching an exception is expensive. In versions of Python prior to 2.0 it was
237238
common to use this idiom::
238239

@@ -352,7 +353,7 @@ will probably run out of file descriptors::
352353
c = f.read(1)
353354

354355
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,
356357
those file objects will only get collected (and closed) at varying and possibly
357358
long intervals.
358359

@@ -376,10 +377,10 @@ Python to work with it.)
376377

377378
Traditional GC also becomes a problem when Python is embedded into other
378379
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()``,
381382
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.
383384

384385

385386
Why isn't all memory freed when CPython exits?
@@ -401,14 +402,15 @@ Why are there separate tuple and list data types?
401402

402403
Lists and tuples, while similar in many respects, are generally used in
403404
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
405406
be of different types which are operated on as a group. For example, a
406407
Cartesian coordinate is appropriately represented as a tuple of two or three
407408
numbers.
408409

409410
Lists, on the other hand, are more like arrays in other languages. They tend to
410411
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
412414
strings representing the files in the current directory. Functions which
413415
operate on this output would generally not break if you added another file or
414416
two to the directory.
@@ -444,9 +446,9 @@ far) under most circumstances, and the implementation is simpler.
444446

445447
Dictionaries work by computing a hash code for each key stored in the dictionary
446448
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
450452
internal array where the value will be stored. Assuming that you're storing
451453
keys that all have different hash values, this means that dictionaries take
452454
constant time -- O(1), in Big-O notation -- to retrieve a key.
@@ -497,7 +499,8 @@ Some unacceptable solutions that have been proposed:
497499

498500
There is a trick to get around this if you need to, but use it at your own risk:
499501
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
501504
hash value for all such wrapper objects that reside in a dictionary (or other
502505
hash based structure), remain fixed while the object is in the dictionary (or
503506
other structure). ::
@@ -528,7 +531,7 @@ is True``) then ``hash(o1) == hash(o2)`` (ie, ``o1.__hash__() == o2.__hash__()``
528531
regardless of whether the object is in a dictionary or not. If you fail to meet
529532
these 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
532535
wrapped list must not change to avoid anomalies. Don't do this unless you are
533536
prepared to think hard about the requirements and the consequences of not
534537
meeting them correctly. Consider yourself warned.
@@ -581,9 +584,9 @@ exhaustive test suites that exercise every line of code in a module.
581584
An appropriate testing discipline can help build large complex applications in
582585
Python as well as having interface specifications would. In fact, it can be
583586
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
585588
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
587590
trivial to check this property in a test suite.
588591

589592
Writing test suites is very helpful, and you might want to design your code to
@@ -599,14 +602,14 @@ Why is there no goto?
599602
In the 1970s people realized that unrestricted goto could lead
600603
to messy "spaghetti" code that was hard to understand and revise.
601604
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`).
605608

606609
One can also use exceptions to provide a "structured goto"
607610
that works even across
608611
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
610613
languages. For example::
611614

612615
class label(Exception): pass # declare a label
@@ -620,7 +623,7 @@ languages. For example::
620623
...
621624

622625
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.
624627

625628

626629
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 ::
652655
Why 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
656659
on the entrance and exit from the block. Some languages have a construct that
657660
looks 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,
683686
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
686689
makes 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
689692
volume) 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?
714717
For technical reasons, a generator used directly as a context manager
715718
would not work correctly. When, as is most common, a generator is used as
716719
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.
718722

719723

720724
Why are colons required for the if/while/def/class statements?

0 commit comments

Comments
 (0)