Python3.6基础知识 内置函数 高阶函数 【五】

版权声明:本文为博主原创文章,转载请标明出处 https://blog.csdn.net/qq_30993595/article/details/83350483

前言

在上一篇Python系列函数文章中讲到了Python的内置高阶函数,如图
在这里插入图片描述

高阶函数英文叫Higher-order function,满足以下特性任意一个即为高阶函数

  • 函数的形参是一个函数名
  • 函数的返回值是一个函数名

而我们通常在Python中说的函数式编程就是指这种高度抽象的编程范式,还有一点就是函数本身也可以赋值给变量,即:变量可以指向函数

接下来从上表中抽一些例子进行讲述

abs(x)

函数的作用想必大家看名字也能知道,它是用来求数字的绝对值的,参数可以是整数和浮点数,如果是复数,就返回大小

在这里插入图片描述

如果我们将该函数赋值给一个变量呢,看看怎么样
在这里插入图片描述

可以看到变量fuc指向了abs,其中的built-in表面abs函数定义在这个模块,而且我们可以通过fuc变量调用它所指向的函数

这里还体现不了高阶函数,上面说了,函数可以赋值给变量,那变量肯定可以当作参数传递给函数,那么就说明一个函数就可以接收另一个函数作为参数,这样高阶函数就形成了,如下

def getValue(x,y,c) :
    return c(x) + c(y)

va = getValue(-1,1,abs)
print(va)

这里函数getValue前两个参数是两个值,后一个参数是函数,这样调用可以知道结果肯定是2,看看打印结果

2

all(iterable)

参数是一个可迭代对象,如果iterable里每个元素都为true,或者iterable为null,那返回true

在这里插入图片描述

在Python中0可以表示false
在这里插入图片描述

any(iterable)

跟all类似,只不过逻辑是只要有一个元素为true就返回true

ascii(object)

这就是将输入对象转换成ascii码

bin(x)

将整数转换为以0b开头的二进制数

bytearray([source[, encoding[, errors]]])

根据参数返回一个字节数组

  • 当不传参数时,返回一个长度为0的字节数组
  • 当传入一个正整数时,返回一个这个数所指定的长度的字节数组,不能传入负数
  • 当传入一个字符串时,同时需要指定编码格式,函数将字符串使用str.encode方法转换成字节数组返回
  • 当source参数为实现了buffer接口的object对象时,那么将使用只读方式将字节读取到字节数组后返回
  • 当source参数是一个可迭代对象,那么这个迭代对象的元素都必须符合0 <= x < 256,以便可以初始化到数组里

在这里插入图片描述

bytes([source[, encoding[, errors]]])

返回值为一个新的不可修改字节数组,每个数字元素都必须在0 - 255范围内,和bytearray函数的具有相同的行为,差别仅仅是返回的字节数组不可修改

操作与上面的类似,只是返回后的数组不能修改

dict([object])

数据结构字典的构造函数

  • 不传参数,返回一个空字典
  • 传入键值对,创建并返回字典
  • 可以传入映射函数创建字典
  • 可以传入可迭代对象创建字典
va = dict()
print(va)
va = dict(a=1,b=2)
print(va)
va = dict(zip(['a','b'],[1,2]))
print(va)
va = dict((('a',1),('b',2)))
print(va)

结果

{}
{'a': 1, 'b': 2}
{'a': 1, 'b': 2}
{'a': 1, 'b': 2}

dir([object])

返回一定范围作用域内的方法,变量,模块,列表等

  • 当不传参数时,返回当前作用域内的变量、方法和定义的类型列表
  • 当参数对象是模块时,返回模块的属性、方法列表
  • 当参数对象是类时,返回类及其子类的属性、方法列表
va = dir()
print(va)
print('=====')
va = dir('math')
print(va)
print('=====')
class MyClass:
    nameClass = 'na'
va = dir(MyClass)
print(va)
['A', 'In', 'MyClass', 'Out', '_', '_1', '_2', '_3', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i14', '_i15', '_i16', '_i17', '_i18', '_i19', '_i2', '_i20', '_i21', '_i22', '_i23', '_i24', '_i25', '_i26', '_i27', '_i28', '_i29', '_i3', '_i30', '_i31', '_i32', '_i33', '_i34', '_i35', '_i36', '_i37', '_i38', '_i39', '_i4', '_i40', '_i41', '_i42', '_i43', '_i44', '_i45', '_i46', '_i47', '_i48', '_i49', '_i5', '_i6', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_oh', 'a', 'data', 'exit', 'fuc', 'getValue', 'get_ipython', 'math', 'quit', 'va']
=====
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
=====
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'nameClass']

eval(expression, globals=None, locals=None)

第一个参数为语句字符串,globals参数和locals参数为可选参数,如果提供,globals参数必需是字典,locals参数为mapping对象,这个函数就是用来指向语句的,代替定义函数

  • 执行动态语句,返回语句执行的值
  • 有globals参数,那它是用来指定代码执行时可以使用的全局变量以及收集代码执行后的全局变量
  • 有locals参数,那它是用来指定代码执行时可以使用的局部变量以及收集代码执行后的局部变量
va = eval('1+2')
print(va)
d = {'id':2}
va = eval('id+2',d)
print(va)
3
4

exec(object[, globals[, locals]])

exec函数和eval函数类似,也是执行动态语句,只不过eval函数只用于执行表达式求值,而exec函数主要用于执行语句块

exec('a=1+2')
print(a)
eval('a=1+2')

exec指向语句块没问题,但是eval就会报错

3
Traceback (most recent call last):

  File "D:\Anaconda3\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-58-ec56cab9a04d>", line 3, in <module>
    eval('a=1+2')

  File "<string>", line 1
    a=1+2
     ^
SyntaxError: invalid syntax

其余两个参数与上面类似

有点无聊了,看张图休息会
在这里插入图片描述

静待更新

猜你喜欢

转载自blog.csdn.net/qq_30993595/article/details/83350483