天天看点

Python训练营笔记 从函数到高级魔法方法 Day7

天池龙珠计划 Python训练营

所记录的知识点

  1. 函数文档
  2. sys.setrecursionlimit getrecursionlimit
  3. lambda 与 map、filter
  4. 自定义的高阶函数

1、函数文档

使用注释为函数创建说明文档
In [1]: def my_fun():
   ...:     '''
   ...:      这是函数文档
   ...:     '''
   ...:     pass
   ...:

In [2]: print(my_fun)
<function my_fun at 0x0000017CAFE0A430>

In [3]: print(my_fun.__doc__)

     这是函数文档


In [4]: help(my_fun)
Help on function my_fun in module __main__:

my_fun()
    这是函数文档           

2、sys.setrecursionlimit getrecursionlimit

设置递归的层数上限
In [1]: import sys

In [2]: help(sys.setrecursionlimit)
Help on built-in function setrecursionlimit in module sys:

setrecursionlimit(limit, /)
    Set the maximum depth of the Python interpreter stack to n.

    This limit prevents infinite recursion from causing an overflow of the C
    stack and crashing Python.  The highest possible limit is platform-
    dependent.


In [3]: help(sys.getrecursionlimit)
Help on built-in function getrecursionlimit in module sys:

getrecursionlimit()
    Return the current value of the recursion limit.

    The recursion limit is the maximum depth of the Python interpreter
    stack.  This limit prevents infinite recursion from causing an overflow
    of the C stack and crashing Python.


In [4]: sys.getrecursionlimit()
Out[4]: 3000

In [5]: sys.setrecursionlimit(10000)

In [6]: sys.getrecursionlimit()
Out[6]: 10000           

3、lambda 与 map、filter

lambda 关键字定义的匿名函数

filter 过滤

map 映射

In [8]: temp = filter(lambda x:x % 2==1,[1,2,3,4,5,6,7,8,9])

In [9]: list(temp)
Out[9]: [1, 3, 5, 7, 9]

In [10]: help(filter)
Help on class filter in module builtins:

class filter(object)
 |  filter(function or None, iterable) --> filter object
 |
 |  Return an iterator yielding those items of iterable for which function(item)
 |  is true. If function is None, return the items that are true.
 |
 |  Methods defined here:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __iter__(self, /)
 |      Implement iter(self).
 |
 |  __next__(self, /)
 |      Implement next(self).
 |
 |  __reduce__(...)
 |      Return state information for pickling.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
           
In [11]: temp = map(lambda x : x ** 2,[1,2,3,4,5,6])

In [12]: list(temp)
Out[12]: [1, 4, 9, 16, 25, 36]

In [13]:

In [13]: help(map)
Help on class map in module builtins:

class map(object)
 |  map(func, *iterables) --> map object
 |
 |  Make an iterator that computes the function using arguments from
 |  each of the iterables.  Stops when the shortest iterable is exhausted.
 |
 |  Methods defined here:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __iter__(self, /)
 |      Implement iter(self).
 |
 |  __next__(self, /)
 |      Implement next(self).
 |
 |  __reduce__(...)
 |      Return state information for pickling.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.           

4、自定义的高阶函数

fun_a 成为 fun_b 的一个参数
In [23]: def my_test(fun,args):
    ...:     return fun(args)
    ...:

In [24]: my_test(lambda x : x+2, 3)
Out[24]: 5

In [25]: my_test(lambda x : x**2 , 6)
Out[25]: 36

In [26]: my_test(sum,[1,23,2])
Out[26]: 26           

欢迎各位同学一起来交流学习心得!