码迷,mamicode.com
首页 > 编程语言 > 详细

python内置函数

时间:2018-09-06 02:36:21      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:python   迭代   方式   def   表示   缺省   property   一个   sig   

abs(x)

返回数字的绝对值。参数可以是整数或浮点数。如果参数是复数,则返回其大小

 

技术分享图片
 1 >>> abs(1)
 2 1
 3 >>> abs(0)
 4 0
 5 >>> abs(-1)
 6 1
 7 >>> abs(1.2)
 8 1.2
 9 >>> abs(-1.2)
10 1.2
View Code

 

  

all(iterable)

如果可迭代对象的所有元素为真,或可迭代对象为空,则返回True,否则返回False

 

技术分享图片
 1 >>> L1 = [1,2,3,4,5]
 2 >>> L2 = []
 3 >>> L3 = [0,1,2,3]
 4 >>> all(L1)
 5 True
 6 >>> all(L2)
 7 True
 8 >>> all(L3)
 9 False
10 >>> all(L4)
View Code

 

any(iterable)

如果可迭代对象其中一个元素为真,就返回True,若可迭代对象为空或所有元素均为假,否则返回False

 

技术分享图片
1 >>> L1,L2,L3
2 ([0, None], [], [0, 1, 2, 3])
3 >>> any(L1)
4 False
5 >>> any(L2)
6 False
7 >>> any(L3)
8 True
View Code

 

ascii(object)

返回一个可打印的对象字符串方式表示,如果是非ascii字符就会输出\x,\u或\U等字符来表示。与python2版本里的repr()是等效的函数。

技术分享图片
 1 >>> ascii(1) 
 2 
 3 1
 4 
 5 >>> ascii(%)
 6 
 7 "‘%‘"
 8 
 9 >>> ascii(中文)  
10 
11 "‘\\u4e2d\\u6587‘"
View Code

 

bin(x)

1. 将一个整形数字转换成二进制字符串

>>> bin(3)
0b11
>>> bin(33)
0b100001

 

 2. 如果参数x不是一个整数,则x必须定义一个 __index__() 方法,并且方法返回值必须是整数。

    2.1 如果对象不是整数,则报错

技术分享图片
1 >>> class A:
2     pass
3 
4 >>> a = A()
5 >>> bin(a) 
6 Traceback (most recent call last):
7   File "<pyshell#15>", line 1, in <module>
8     bin(a)
9 TypeError: A object cannot be interpreted as an integer
View Code

2.2 如果对象定义了__index__方法,但返回值不是整数,报错

技术分享图片
 1 >>> class B:
 2     def __index__(self):
 3         return "3"
 4 
 5 >>> b = B()
 6 >>> bin(b)
 7 Traceback (most recent call last):
 8   File "<pyshell#21>", line 1, in <module>
 9     bin(b)
10 TypeError: __index__ returned non-int (type str)
View Code

2.3 对象定义了__index__方法,且返回值是整数,将__index__方法返回值转换成二进制字符串

技术分享图片
1 >>> class C:
2     def __index__(self):
3         return 3
4 
5 >>> c = C()
6 >>> bin(c)
7 0b11
View Code

 

class bool([x])

1. 返回值为True或者False的布尔值

 2. 参数如果缺省,则返回False

技术分享图片
1 >>> bool()
2 False
3 >>> bool(0)
4 False
5 >>> bool(None)
6 False
7 >>> bool(1)
8 True
View Code

只要参数X,非0、非空、非None结果就是True,否则为False

 

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

 1. 返回值为一个新的字节数组

 2. 当3个参数都不传的时候,返回长度为0的字节数组

技术分享图片
>>> a =  bytearray()
>>> a
bytearray(b‘‘)
>>> len(a) 
0
View Code

 3. 当source参数为字符串时,encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组

技术分享图片
>>> bytearray(中文)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string argument without an encoding
>>> bytearray(中文,utf-8)
bytearray(b\xe4\xb8\xad\xe6\x96\x87)
View Code

 4. 当source参数为整数时,返回这个整数所指定长度的空字节数组

技术分享图片
>>> bytearray(4)
bytearray(b\x00\x00\x00\x00)
>>> bytearray(-4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative count
View Code

 5. 当source参数为实现了buffer接口的object对象时,那么将使用只读方式将字节读取到字节数组后返回

 6. 当source参数是一个可迭代对象,那么这个迭代对象的元素都必须符合0 <= x < 256,以便可以初始化到数组里

技术分享图片
>>> bytearray([1,2,3])
bytearray(b\x01\x02\x03)
>>> bytearray([1,2,3,4])
bytearray(b\x01\x02\x03\x04)
>>> bytearray([1,2,3,4,255])
bytearray(b\x01\x02\x03\x04\xff)
>>> bytearray([1,2,3,4,255,256])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: byte must be in range(0, 256)
View Code

 

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

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

2. 当3个参数都不传的时候,返回长度为0的字节数组

技术分享图片
>>> a =  bytes()
>>> len(a)
0
View Code

3. 当source参数为字符串时,encoding参数也必须提供,函数将字符串使用str.encode方法转换成字节数组

技术分享图片
>>> bytes(中文) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string argument without an encoding
>>> bytes(中文,utf-8) 
b\xe4\xb8\xad\xe6\x96\x87
View Code

 4. 当source参数为整数时,返回这个整数所指定长度的空字节数组

技术分享图片
>>> bytes(4)
b\x00\x00\x00\x00
>>> bytes(-4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative count
View Code

5. 当source参数为实现了buffer接口的object对象时,那么将使用只读方式将字节读取到字节数组后返回

6. 当source参数是一个可迭代对象,那么这个迭代对象的元素都必须符合0 <= x < 256,以便可以初始化到数组里

技术分享图片
>>> bytes([1,2,3])
b\x01\x02\x03
>>> bytes([1,2,3,255])
b\x01\x02\x03\xff
>>> bytes([1,2,3,255,256])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: bytes must be in range(0, 256)
View Code

 7. 返回数组不可修改

技术分享图片
>>> b = bytes(5) 
>>> b
b\x00\x00\x00\x00\x00
>>> len(b)
5
>>> b[0]
0
>>> b[1]
0
>>> b[0]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bytes object does not support item assignment
>>> b[1]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bytes object does not support item assignment
View Code

 

callable(object)

1. 方法用来检测对象是否可被调用,可被调用指的是对象能否使用()括号的方法调用。

技术分享图片
>>> callable(callable)
True
>>> callable(1)       
False
>>> 1()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int object is not callable
 >>> callable(bin)
True
>>>
View Code

2. 可调用对象,在实际调用也可能调用失败;但是不可调用对象,调用肯定不成功。

3. 类对象都是可被调用对象,类的实例对象是否可调用对象,取决于类是否定义了__call__方法。

技术分享图片
>>> class A: #定义类A
    pass

>>> callable(A) #类A是可调用对象
True
>>> a = A() #调用类A
>>> callable(a) #实例a不可调用
False
>>> a() #调用实例a失败
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    a()
TypeError: A object is not callable


>>> class B: #定义类B
    def __call__(self):
        print(instances are callable now.)

        
>>> callable(B) #类B是可调用对象
True
>>> b = B() #调用类B
>>> callable(b) #实例b是可调用对象
True
>>> b() #调用实例b成功
instances are callable now.
View Code

 

python内置函数

标签:python   迭代   方式   def   表示   缺省   property   一个   sig   

原文地址:https://www.cnblogs.com/liliyang/p/9595667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!