测试代码使用python3.7执行
input
input([prompt])如果指定了prompt参数,将会把prompt输出到标准输出,后面不加新的空行,prompt是一个提示符。然后函数会读取一行并转换为str(去掉后面的空行)并返回。
测试代码:
def test_input():
input_str = input()
print("input: ", input_str)
input2 = input("please input the num: ")
print("input2: ", input2, "; type: ", type(input2))
test_input()
测试结果:
abcdef
input: abcdef
please input the num: 999
input2: 999 ; type: <class 'str'>
int
class int([x]),class int(x, base=10)将一个数字或字符串转换为整型数,如果没有参数则返回0。base指定进制数,默认是10进制。base指定的是x的进制。
测试代码:
def test_int():
int0 = int()
print("int0: ", int0)
int1 = int(1)
print("int1: ", int1)
int5 = int(5.8)
print("int5: ", int5)
int10 = int('16', 16)
print("int10: ", int10)
int11 = int('0xa', 16)
print("int11: ", int11)
int12 = int("11", 8)
print("int12: ", int12)
test_int()
测试结果:
int0: 0
int1: 1
int5: 5
int10: 22
int11: 10
int12: 9
isinstance
isinstance(object, classinfo),如果object是classinfo或者其(直接,间接,虚拟)子类的的一个实例则返回True。如果不是给定类型的对象则返回False。如果classinfo是一个Tuple,object是任一类型的子类则返回True。Type()可以获取对象的类型。下面进行介绍。
测试代码:
def test_isinstance():
print("1 is instance of int: ", isinstance(1, int))
print("1 is instance of str: ", isinstance(1, str))
print("1 is instance of (int, float, dict, str): ", isinstance(1, (int, float, dict, str)))
print("1 is instance of (list, float, dict, str): ", isinstance(1, (list, float, dict, str)))
obj = TestClass()
print("obj is instance of TestClass: ", isinstance(obj, TestClass))
test_isinstance()
测试结果:
1 is instance of int: True
1 is instance of str: False
1 is instance of (int, float, dict, str): True
1 is instance of (list, float, dict, str): False
obj is instance of TestClass: True
type
type有两种方式:class type(object)和class type(name, bases, dict)。只有一个参数的时候返回的是该对象的class,相当于object.__class__。当使用多个参数的type时返回一个新的类型。
type和isinstance有个区别就是type只能获取到直接类别,不能够获取父类,在判定的时候用isinstance会好一些。
测试代码:
def test_type():
print("type 1: ", type(1))
print("type 'abc': ", type('abc'))
obj = SubClass()
print("type obj: ", type(obj))
print("obj is instance of TestCless: ", isinstance(obj, TestClass))
print("obj type is TestClass: ", type(obj) == TestClass)
print("obj is instance of SubClass: ", isinstance(obj, SubClass))
print("obj type is SubClass: ", type(obj) == SubClass)
new_type = type("newClass", (MyClass,), dict(abc= 88, count=77))
print("new_type: ", new_type)
print("new_type: ", type(new_type))
print("new_type: ", new_type.__class__)
print("new_type: ", new_type.abc)
print("new_type: ", new_type.count)
print("new_type: ", new_type.m)
test_type()
测试结果:
type 1: <class 'int'>
type 'abc': <class 'str'>
type obj: <class '__main__.SubClass'>
obj is instance of TestCless: True
obj type is TestClass: False
obj is instance of SubClass: True
obj type is SubClass: True
new_type: <class '__main__.newClass'>
new_type: <class 'type'>
new_type: <class 'type'>
new_type: 88
new_type: 77
new_type: 100
issubclass
issubclass(class, classinfo)如果 class 是classinfo 的(直接、间接或虚拟)子类,则返回 True。 一个类是它自己的一个子类。 classinfo可以是一个tuple,会检查里面的所有类型。
测试代码:
class TestClass:
def __init__(self) -> None:
self.num = 9
class SubClass(TestClass):
def __init__(self) -> None:
super(SubClass).__init__()
self.count = 8
def test_isinstance():
print("1 is instance of int: ", isinstance(1, int))
print("1 is instance of str: ", isinstance(1, str))
print("1 is instance of (int, float, dict, str): ", isinstance(1, (int, float, dict, str)))
print("1 is instance of (list, float, dict, str): ", isinstance(1, (list, float, dict, str)))
obj = TestClass()
print("obj is instance of TestClass: ", isinstance(obj, TestClass))
test_isinstance()
测试结果:
int is subclass of object: True
int is subclass of str: False
subclass is subclass of TestClass: True
iter
iter(object[, sentinel]),返回一个迭代器对象。该函数有两种形式,一个参数和两个参数。当只传入一个参数的时候,参数object必须是一个集合对象,且必须支持__iter__()函数和__getitem__()函数的任意一个。如果传入两个参数,object必须是一个可调用对象,这中情况下迭代器每次调用__next__() 方法时都会调用不带参数的object对象, 如果返回的值等于 sentinel,将抛出StopIteration,否则将返回该值。
测试代码:
def test_iter():
class callableCls:
def __init__(self) -> None:
self.nums = range(10)
self.i = 0
def __call__(self):
self.i += 1
return self.nums[self.i]
num = [1, 2, 3, 4, 5, 6, 7]
for i in iter(num):
print("i: ", i)
dicts = {"tom": 21, "jerry": 22}
for i in iter(dicts):
print("dict i: ", i)
for i in iter(callableCls(), 6):
print("test callable i: ", i)
test_iter()
测试结果:
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
dict i: tom
dict i: jerry
test callable i: 1
test callable i: 2
test callable i: 3
test callable i: 4
test callable i: 5
len
len(s)返回对象中包含的成员数量。 参数可以是一个序列(如string、bytes、tuple、list或range)或集合(如dict、set或frozen set)。
测试代码:
def test_len():
l = [1, 2 ,3, 4, 5, 6]
print("len of list: ", len(l))
d = {"tom": 21, "jerry": 22}
print("len of dict: ", len(d))
test_len()
测试结果:
len of list: 6
len of dict: 2
list
class list([iterable])实际上不是一个函数,而是一个可变序列类型。将一个序列转换为list。
测试代码:
def test_list():
t = (1, 2, 3, 4, 5)
print("tuple to list: ", list(t), "; t type: ", type(t), "; list(t) type: ", type(list(t)))
dicts = {"tom": 21, "jerry": 22}
print("dict to list: ", list(dicts))
test_list()
测试结果:
tuple to list: [1, 2, 3, 4, 5] ; t type: <class 'tuple'> ; list(t) type: <class 'list'>
dict to list: ['tom', 'jerry']
long
long()将数字或字符串转换为长整型,python3中已删除,不在介绍了。
map
map(function, iterable, …)返回一个迭代器,并在iterable里面的每个成员都执行function,返回结果。 如果传递了多个迭代参数,则每次从这些参数里面取一个值作为function的参数。 对于多个可迭代对象,迭代器会在最短的可迭代对象耗尽时停止。
测试代码:
def test_map():
def add1(num):
return num + 1
def add2(num1, num2):
return num1 + num2
l = [1, 2, 3, 4, 5, 6]
for n in map(add1, l):
print("n: ", n)
l1 = (9, 8, 7, 6)
for n in map(add2, l, l1):
print("n: ", n)
test_map()
测试结果:
n: 2
n: 3
n: 4
n: 5
n: 6
n: 7
n: 10
n: 10
n: 10
n: 10