python3内置函数与标准库的关系_Python3标准库(一) 内置函数

Python中内置了很多的函数,而学习标准库我们需要事先熟悉这些内置函数,所以在这里一个个进行介绍。有些函数比较简单,只给予文字描述,而有些函数会给出示例代码帮助理解。

>>> bin(9)

'0b1001'

>>> bool()

False

>>> bool('fedora')

True

>>> a = bytearray(b'after')

>>> list(a) # convert a bytearray object into a list of integers

[97, 102, 116, 101, 114]

不可变版本:

>>> a = bytes('after', 'UTF-8')

>>> list(a) # convert a bytes object into a list of integers

[97, 102, 116, 101, 114]

call()方法,则它是可以调用的。

>>> f = lambda x,y : x+y

>>> callable(f) # f是函数对象,可调用

True

>>> a = 10 # a 不可调用

>>> callable(a)

False

ord()正好相反。

>>> chr(97)

'a'

del object.name。

(a//b, a%b)。

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']

>>> list(enumerate(seasons))

[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

>>> for x in enumerate(seasons): # 遍历

... print(x, end=' ')

...

(0, 'Spring') (1, 'Summer') (2, 'Fall') (3, 'Winter')相当于:

def enumerate(sequence, start=0):

n = start

for elem in sequence:

yield n, elem

n += 1

>>> x = 1

>>> eval('x+1')

2

>>> x = 1

>>> exec('x += 10')

>>> x

11

>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]

>>> f = lambda x: x%2==0

>>> list(filter(f, lst))

[2, 4, 6, 8]

>>> format(3.1415926, '7.3g')

' 3.14'

全局符号和对应值的字典。

注意:所有不可变的内置类型都是 hashable 的,比如 string,tuple;所有可变的内置类型都是 unhashable 的,比如 list,dict(即没有__hash__()方法)。

31、id():返回一个对象的 id 身份,可以看作该对象的内存地址。

globals() 对应。

>>> it = map(lambda x: x*2, [1,2,3,4])

>>> list(it)

[2, 4, 6, 8]

chr(i)相反。

>>> ord('a')

97

pow(x, y)相当于x**y,pow(x, y, z)相当于pow(x, y) % z。

class C:

def __init__(self):

self.__x = None

def getx(self):

return self.__x

def setx(self, value):

self.__x = value

def delx(self):

del self.__x

x = property(getx, setx, delx, "I'm the 'x' property.")为了操作数据成员 __x,我们需要使用 getx、setx、delx 方法,很麻烦。但是如果通过 property 函数将方法绑定到成员x,那么当获取成员x的值时,就会调用getx函数;当给成员x赋值时,就会调用setx函数;当删除x时,就会调用delx函数:

c = C()

print(c.x) # 相当于c.getx()

c.x = 20 # 相当于c.setx(20)

del c.x # 相当于c.delx()

这样通过 x 间接调用方法操作 __x 就方便多了。

>>> round(1.75368, 3)

1.754

class C:

@staticmethod

def f(arg1, arg2, ...): ...

类的继承中,通常需要调用父类的构造方法,以初始化父类部分,有两种方法能达到这个目的。

方法一:调用未绑定的父类构造方法

class A:

def __init__(self):

self.a = 'A_method'

class B:

def __init__(self):

self.b = 'B_method'

class C(A, B):

def __init__(self):

A.__init__(self)

B.__init__(self)

# ...其他超类

self.c = 'C_method'

方法二:使用super函数

class A:

def __init__(self):

super().__init__()

self.a = 'A_method'

class B:

def __init__(self):

super().__init__()

self.b = 'B_method'

class C(A, B):

def __init__(self):

super().__init__() # 等价于super(C,self).__init__()

self.c = 'C_method'

可以看出,方法一更直观,但是方法二使用 super 函数可以一次初始化所有超类(但要确保所有的超类的构造方法都使用了super函数)。当继承结构很复杂时,方法二明显更适用,当然 super 不仅可以用于构造方法还可以用于其他方法。

使用 super 还有一个好处,就是当改变父类名时,不需要再去修改其他地方,便于代码的维护。

object.__class__一样。

locals()一样。

附:reduce函数

在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在functools模块里。函数原型如下:

reduce(function, iterable[, initializer])function 必须是二元函数,

在省略第三个参数的情况下,函数先对 iterable 中的第1,2个数据进行操作,得到的结果再与第三个数据用 function() 函数运算……依次类推,最后得到一个结果。

如果初始值 initializer 给定,第一次调用会是 initializer 和第一个元素而不是序列的头两个元素。

>>> from functools import reduce

>>> reduce(lambda x,y: x+y, [1,2,3,4,5])

15

>>> reduce(lambda x,y: x+y, [1,2,3,4,5], 10)

25map用于映射,reduce用于归并。

(全文完)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值