Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
db8707c8
Kaydet (Commit)
db8707c8
authored
Agu 07, 2018
tarafından
Sergey Fedoseev
Kaydeden (comit)
Mariatta
Agu 07, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Make code examples in Functional Programming HOWTO to be PEP 8 compliant. (GH-8646)
üst
d2ac4002
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
23 deletions
+23
-23
functional.rst
Doc/howto/functional.rst
+23
-23
No files found.
Doc/howto/functional.rst
Dosyayı görüntüle @
db8707c8
...
...
@@ -198,7 +198,7 @@ for it.
You can experiment with the iteration interface manually:
>>> L = [1,
2,
3]
>>> L = [1,
2,
3]
>>> it = iter(L)
>>> it #doctest: +ELLIPSIS
<...iterator object at ...>
...
...
@@ -229,7 +229,7 @@ iterator. These two statements are equivalent::
Iterators can be materialized as lists or tuples by using the :func:`list` or
:func:`tuple` constructor functions:
>>> L = [1,
2,
3]
>>> L = [1,
2,
3]
>>> iterator = iter(L)
>>> t = tuple(iterator)
>>> t
...
...
@@ -238,10 +238,10 @@ Iterators can be materialized as lists or tuples by using the :func:`list` or
Sequence unpacking also supports iterators: if you know an iterator will return
N elements, you can unpack them into an N-tuple:
>>> L = [1,
2,
3]
>>> L = [1,
2,
3]
>>> iterator = iter(L)
>>> a,
b,
c = iterator
>>> a,
b,
c
>>> a,
b,
c = iterator
>>> a,
b,
c
(1, 2, 3)
Built-in functions such as :func:`max` and :func:`min` can take a single
...
...
@@ -411,7 +411,7 @@ lengths of all the sequences. If you have two lists of length 3, the output
list is 9 elements long:
>>> seq1 = 'abc'
>>> seq2 = (1,
2,
3)
>>> seq2 = (1,
2,
3)
>>> [(x, y) for x in seq1 for y in seq2] #doctest: +NORMALIZE_WHITESPACE
[('a', 1), ('a', 2), ('a', 3),
('b', 1), ('b', 2), ('b', 3),
...
...
@@ -479,7 +479,7 @@ Here's a sample usage of the ``generate_ints()`` generator:
File "stdin", line 2, in generate_ints
StopIteration
You could equally write ``for i in generate_ints(5)``, or ``a,
b,
c =
You could equally write ``for i in generate_ints(5)``, or ``a,
b,
c =
generate_ints(3)``.
Inside a generator function, ``return value`` causes ``StopIteration(value)``
...
...
@@ -695,17 +695,17 @@ truth values of an iterable's contents. :func:`any` returns ``True`` if any ele
in the iterable is a true value, and :func:`all` returns ``True`` if all of the
elements are true values:
>>> any([0,
1,
0])
>>> any([0,
1,
0])
True
>>> any([0,
0,
0])
>>> any([0,
0,
0])
False
>>> any([1,
1,
1])
>>> any([1,
1,
1])
True
>>> all([0,
1,
0])
>>> all([0,
1,
0])
False
>>> all([0,
0,
0])
>>> all([0,
0,
0])
False
>>> all([1,
1,
1])
>>> all([1,
1,
1])
True
...
...
@@ -764,7 +764,7 @@ which defaults to 0, and the interval between numbers, which defaults to 1::
a provided iterable and returns a new iterator that returns its elements from
first to last. The new iterator will repeat these elements infinitely. ::
itertools.cycle([1,
2,3,4,
5]) =>
itertools.cycle([1,
2, 3, 4,
5]) =>
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
:func:`itertools.repeat(elem, [n]) <itertools.repeat>` returns the provided
...
...
@@ -875,7 +875,7 @@ iterable's results. ::
iterators and returns only those elements of *data* for which the corresponding
element of *selectors* is true, stopping whenever either one is exhausted::
itertools.compress([1,
2,3,4,
5], [True, True, False, False, True]) =>
itertools.compress([1,
2, 3, 4,
5], [True, True, False, False, True]) =>
1, 2, 5
...
...
@@ -1035,7 +1035,7 @@ first calculation. ::
Traceback (most recent call last):
...
TypeError: reduce() of empty sequence with no initial value
>>> functools.reduce(operator.mul, [1,
2,
3], 1)
>>> functools.reduce(operator.mul, [1,
2,
3], 1)
6
>>> functools.reduce(operator.mul, [], 1)
1
...
...
@@ -1045,9 +1045,9 @@ elements of the iterable. This case is so common that there's a special
built-in called :func:`sum` to compute it:
>>> import functools, operator
>>> functools.reduce(operator.add, [1,
2,3,
4], 0)
>>> functools.reduce(operator.add, [1,
2, 3,
4], 0)
10
>>> sum([1,
2,3,
4])
>>> sum([1,
2, 3,
4])
10
>>> sum([])
0
...
...
@@ -1057,11 +1057,11 @@ write the obvious :keyword:`for` loop::
import functools
# Instead of:
product = functools.reduce(operator.mul, [1,
2,
3], 1)
product = functools.reduce(operator.mul, [1,
2,
3], 1)
# You can write:
product = 1
for i in [1,
2,
3]:
for i in [1,
2,
3]:
product *= i
A related function is :func:`itertools.accumulate(iterable, func=operator.add)
...
...
@@ -1069,10 +1069,10 @@ A related function is :func:`itertools.accumulate(iterable, func=operator.add)
returning only the final result, :func:`accumulate` returns an iterator that
also yields each partial result::
itertools.accumulate([1,
2,3,4,
5]) =>
itertools.accumulate([1,
2, 3, 4,
5]) =>
1, 3, 6, 10, 15
itertools.accumulate([1,
2,3,4,
5], operator.mul) =>
itertools.accumulate([1,
2, 3, 4,
5], operator.mul) =>
1, 2, 6, 24, 120
...
...
@@ -1156,7 +1156,7 @@ But it would be best of all if I had simply used a ``for`` loop::
Or the :func:`sum` built-in and a generator expression::
total = sum(b for a,b in items)
total = sum(b for a,
b in items)
Many uses of :func:`functools.reduce` are clearer when written as ``for`` loops.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment