xy之python文档反思

2017-06-22  本文已影响0人  __XY__

这个系列主要是文档与自己的用法之间的出入的反思,以后不光需要多练,仍需要更多的反思

More Control Flow Tools

>>> for w in words[:]:  # Loop over a slice copy of the entire list.
...     if len(w) > 6:
...         words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print(n, 'equals', x, '*', n//x)
...             break
...     else:
...         # loop fell through without finding a factor
...         print(n, 'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
i = 5
def f(arg=i):
    print(arg)
i = 6
f()
def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L
>>> def my_function():
...     """Do nothing, but document it.
...
...     No, really, it doesn't do anything.
...     """
...     pass
...
>>> print(my_function.__doc__)
Do nothing, but document it.

    No, really, it doesn't do anything.
my_function.__doc__

参考

https://docs.python.org/3.5/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

上一篇 下一篇

猜你喜欢

热点阅读