Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5b266e

Browse files
committed
Streamline FAQ entry about the ternary operator, and suggest using io.StringIO for a mutable unicode container.
1 parent 9cb41df commit c5b266e

1 file changed

Lines changed: 20 additions & 54 deletions

File tree

Doc/faq/programming.rst

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -679,61 +679,21 @@ are not truly operators but syntactic delimiters in assignment statements.
679679
Is there an equivalent of C's "?:" ternary operator?
680680
----------------------------------------------------
681681

682-
Yes, this feature was added in Python 2.5. The syntax would be as follows::
682+
Yes, there is. The syntax is as follows::
683683

684684
[on_true] if [expression] else [on_false]
685685

686686
x, y = 50, 25
687-
688687
small = x if x < y else y
689688

690-
For versions previous to 2.5 the answer would be 'No'.
691-
692-
.. XXX remove rest?
693-
694-
In many cases you can mimic ``a ? b : c`` with ``a and b or c``, but there's a
695-
flaw: if *b* is zero (or empty, or ``None`` -- anything that tests false) then
696-
*c* will be selected instead. In many cases you can prove by looking at the
697-
code that this can't happen (e.g. because *b* is a constant or has a type that
698-
can never be false), but in general this can be a problem.
699-
700-
Tim Peters (who wishes it was Steve Majewski) suggested the following solution:
701-
``(a and [b] or [c])[0]``. Because ``[b]`` is a singleton list it is never
702-
false, so the wrong path is never taken; then applying ``[0]`` to the whole
703-
thing gets the *b* or *c* that you really wanted. Ugly, but it gets you there
704-
in the rare cases where it is really inconvenient to rewrite your code using
705-
'if'.
706-
707-
The best course is usually to write a simple ``if...else`` statement. Another
708-
solution is to implement the ``?:`` operator as a function::
689+
Before this syntax was introduced in Python 2.5, a common idiom was to use
690+
logical operators::
709691

710-
def q(cond, on_true, on_false):
711-
if cond:
712-
if not isfunction(on_true):
713-
return on_true
714-
else:
715-
return on_true()
716-
else:
717-
if not isfunction(on_false):
718-
return on_false
719-
else:
720-
return on_false()
721-
722-
In most cases you'll pass b and c directly: ``q(a, b, c)``. To avoid evaluating
723-
b or c when they shouldn't be, encapsulate them within a lambda function, e.g.:
724-
``q(a, lambda: b, lambda: c)``.
692+
[expression] and [on_true] or [on_false]
725693

726-
It has been asked *why* Python has no if-then-else expression. There are
727-
several answers: many languages do just fine without one; it can easily lead to
728-
less readable code; no sufficiently "Pythonic" syntax has been discovered; a
729-
search of the standard library found remarkably few places where using an
730-
if-then-else expression would make the code more understandable.
731-
732-
In 2002, :pep:`308` was written proposing several possible syntaxes and the
733-
community was asked to vote on the issue. The vote was inconclusive. Most
734-
people liked one of the syntaxes, but also hated other syntaxes; many votes
735-
implied that people preferred no ternary operator rather than having a syntax
736-
they hated.
694+
However, this idiom is unsafe, as it can give wrong results when *on_true*
695+
has a false boolean value. Therefore, it is always better to use
696+
the ``... if ... else ...`` form.
737697

738698

739699
Is it possible to write obfuscated one-liners in Python?
@@ -852,15 +812,21 @@ the :ref:`string-formatting` section, e.g. ``"{:04d}".format(144)`` yields
852812
How do I modify a string in place?
853813
----------------------------------
854814

855-
You can't, because strings are immutable. If you need an object with this
856-
ability, try converting the string to a list or use the array module::
815+
You can't, because strings are immutable. In most situations, you should
816+
simply construct a new string from the various parts you want to assemble
817+
it from. However, if you need an object with the ability to modify in-place
818+
unicode data, try using a :class:`io.StringIO` object or the :mod:`array`
819+
module::
857820

858821
>>> s = "Hello, world"
859-
>>> a = list(s)
860-
>>> print(a)
861-
['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
862-
>>> a[7:] = list("there!")
863-
>>> ''.join(a)
822+
>>> sio = io.StringIO(s)
823+
>>> sio.getvalue()
824+
'Hello, world'
825+
>>> sio.seek(7)
826+
7
827+
>>> sio.write("there!")
828+
6
829+
>>> sio.getvalue()
864830
'Hello, there!'
865831

866832
>>> import array

0 commit comments

Comments
 (0)