1 说明
以下解释来源于官网和个人理解,官网的英文说明个人觉得理解起来更加准确,更加容易懂。翻译过来的中文的确每个字都认起来都毫无障碍,但整体意思总是怪怪的,或者理解起来不够准确。或许编写文档的专业人士用的是英语,人家自然会用英语的方式来直击灵魂深处地解释,而翻译通常是基于字面上的机械翻译
https://docs.python.org/3/library/functions.html
1 len()
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set)
In [1]: len({1:11,2:22})
Out[1]: 2
In [4]: len(set([1,2,3]))
Out[4]: 3
In [5]: word_1 = '中文'
In [6]: word_2 = 'abc'
In [7]: len(word_1)
Out[7]: 2
In [8]: len(word_2)
Out[8]: 3
In [9]: word_1_byte = word_1.encode('utf-8')
In [10]: len(word_1_byte)
Out[10]: 6
In [11]: word_1_byte
Out[11]: b'\xe4\xb8\xad\xe6\x96\x87'
In [12]: word_2_byte = word_2.encode('utf-8')
In [13]: len(word_2_byte)
Out[13]: 3
In [14]: word_2_byte
Out[14]: b'abc'
可以看到,py对象变为字节时,item发生了变化,其长度也变化了
这里想到了一个问题,列表等对象如何转化为bytes
2 type()
class type(name, bases, dict, **kwds)
With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.class.
The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.
In [22]: type("中文")
Out[22]: str
In [23]: "中文".__class__
Out[23]: str
In [27]: def FunA():
...: pass
...:
In [28]: type(FunA)
Out[28]: function
In [29]: class SelfClassA():
...: pass
...:
In [30]: type(SelfClassA)
Out[30]: type
In [36]: class_a = SelfClassA()
In [37]: type(class_a)
Out[37]: __main__.SelfClassA
3 isinstance()
相较于type(),官方更推荐使用isinstance。在有子类、父类的情况时,type()会判断不出来子类的对象其实也属于父类
Return True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtual) subclass thereof. If object is not an object of the given type, the function always returns False. If classinfo is a tuple of type objects (or recursively, other such tuples) or a Union Type of multiple types, return True if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised. TypeError may not be raised for an invalid type if an earlier check succeeds
In [38]: isinstance(class_a,SelfClassA)
Out[38]: True
In [39]: isinstance("中文",str)
Out[39]: True
In [40]: isinstance("中文",int)
Out[40]: False
In [41]: isinstance("中文",(dict,str))
Out[41]: True
In [42]: isinstance({},(dict,str))
Out[42]: True
4int()
class int(x=0)
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are given.
For floating point numbers, this truncates towards zero
参数支持内容为数字的str,float,不支持的参数会引发ValueError
In [45]: int('11')
Out[45]: 11
In [46]: int(11.4)
Out[46]: 11
In [47]: int(11.5)
Out[47]: 11
In [48]: int(11.6)
Out[48]: 11
In [49]: int(11.9)
Out[49]: 11
对于float的int()会直接去掉小数部分
5 float()
class float(x=0.0)
Return a floating point number constructed from a number or string x.
参数支持float int,内容为数字的字符串,非法参数会有ValueError
In [50]: float(11)
Out[50]: 11.0
In [51]: float('11')
Out[51]: 11.0
6 chr()和ord()
chr()把一个unicode字符转化成该字符对应的一个十进制数字。ord()则是反过来
7 hex()和bin()
hen()将一个整数转化成小写的十六进制字符串
bin()是将一个整数转化成二进制字符串
8 abs()
Return the absolute value of a number. The argument may be an integer, a floating point number, or an object implementing . If the argument is a complex number, its magnitude is returned.abs()
返回绝对值,参数可以是int float等
9 round(number, ndigits=None)
对数字number在小数位ndigits上进行四舍五入,ndigits没有输入或者是None的话则是返回其最接近的整数
10 max() 和min()
max(iterable, *, key=None)¶
max(iterable, *, default, key=None)
max(arg1, arg2, *args, key=None)
Return the largest item in an iterable or the largest of two or more arguments
参数可以是一个可迭代对象或者多个位置参数
min()则相反,返回最小值
min(iterable, *, key=None)¶
min(iterable, *, default, key=None)
min(arg1, arg2, *args, key=None)
Return the smallest item in an iterable or the smallest of two or more arguments.
11 sum()
sum(iterable, /, start=0)
Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.
12 dir()
dir()
dir(object)
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
返回对象的属性(函数 类 变量等),如果没有指定参数,就是针对当前该函数所在的范围
如 输入dir() 我这里会输出一些内部变量和之前定义的变量
一个对象可自定义一个__dir__()方法,以此来规定调用此方法时要返回哪些数据
如果没有定义__dir__(),则会根据obj.__dict__来返回数据
13 id(obj)
获取对象的内存地址,如果两个对象的id值相同,则说明他们是同一个对象
14 input(prompt)
提示用户输入并获取输入,提示词为prompt
15 open(path)
读写文件时常用该函数
16 eval()
参数必须是字符串,个人理解是把字符串的python表达式,去掉字符串引号,放到py里面来执行
因为某种原因,py代码被提前写到字符串里面,需要的时候就用该函数来执行提前写好的代码
17 range()
class range(stop)
class range(start, stop, step=1)
从start开始,生成一个整数序列,间隔是step,不包括stop
从0输出到100
for i in range(101):
print(i)
标签:11,内置,word,函数,python,object,type,class,Out
From: https://www.cnblogs.com/MyRecords/p/17385833.html