多一句没有,少一句不行,用最短的时间,教会你有用的技术。
all()
✅语法:
Return True if all elements of the iterable are true (or if the iterable is empty).
等价于下面这个:
def all(iterable):
for element in iterable:
if not element:
return False
return True
✅ 解释:
如果iterable
中的所有元素都为真(或者iterable
为空),则返回True
。
✅示例:
list1=[1,2,3,4,5,6]
list2=[0,2,3,0,5,6]
list3=[0,0,0,0,0,0]
print(all(list1))
print(all(list2))
print(all(list3))
# True
# False
# False
any()
✅语法:
Return True if any element of the iterable is true. If the iterable is empty, return False.
✅解释:
如果iterable
中的任何元素为真,则返回真。如果可迭代对象为空,则返回False
。
✅示例:
list1=[1,2,3,4,5,6]
list2=[0,2,3,0,5,6]
list3=[0,0,0,0,0,0]
print(any(list1))
print(any(list2))
print(any(list3))
# True
# True
# False
bin()
✅语法:
Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a Python int object, it has to define an index() method that returns an integer.
✅解释:
将一个整数转换为一个前缀为“0b”的二进制字符串。结果是一个有效的Python
表达式。如果x不是Python
int
对象,它必须定义一个返回整数的__index__()
方法。
✅示例:
这个做算法题的时候会经常用到。
print(bin(3))
print(bin(-10))
# 0b11
# -0b1010
callable
见过无敌哥在写装饰器得时候用过,用得真是太妙了。
✅语法:
如果传入得参数是可调用的,则返回True
,否则返回False
。如果返回True,调用仍然有可能失败,但如果返回False
,调用object
将永远不会成功。注意:类是可调用的(调用类会返回一个新的实例);如果实例的类具有__call__()
方法,则实例是可调用的。
Return True if the object argument appears callable, False if not. If this returns True, it is still possible that a call fails, but if it is False, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a call() method.
✅多说两句:
参考链接:
知道你看了上面的解释依然不会用。我们再来阐述一下:
一个可callable的对象是指可以被调用执行的对象,并且可以传入参数, 用另一个简单的描述方式,只要可以在一个对象的后面使用小括号来执行代码,那么这个对象就是callable对象,下面列举callable对象的种类:函数,类,类里的函数,实现了call方法的实例对象.
函数
def test():
print('ok')
print(callable(test)) # True
test() # ok
类
class Stu(object):
def __init__(self, name):
self.name = name
print(callable(Stu)) # True
print(Stu('小明').name) # 小明
类里的方法
类里的方法也是用def定义的,本质上也是函数
from inspect import isfunction, ismethod
class Stu(object):
def __init__(self, name):
self.name = name
def run(self):
print('{name} is running'.format(name=self.name))
print(isfunction(Stu.run)) # True
stu = Stu("小明")
stu.run() # 小明 is running
使用isfunction函数可以判断一个对象是否是函数,run方法也是可调用对象
实现了call方法的实例对象
lass Stu(object):
def __init__(self, name):
self.name = name
def __call__(self, *args, **kwargs):
self.run()
def run(self):
print('{name} is running'.format(name=self.name))
stu = Stu('小明')
print(callable(stu)) # True
stu() # 小明 is ru
enumerate
这个用的实在是太频繁了。
语法:
Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.
解释:
返回一个枚举对象。Iterable必须是序列、迭代器或其他支持迭代的对象。enumerate()返回的迭代器的next()方法返回一个元组,其中包含一个计数(从start开始,默认为0)和通过迭代iterable获得的值。
示例:
id
这个就不多说了,以后用到详细介绍。
✅解释:
返回一个对象的“标识”。
isinstance & type
✅语法:
isinstance:如果
object
参数是classinfo
参数的实例,或其(直接、间接或虚)子类的实例,则返回True
。如果object
不是给定类型的对象,函数总是返回False
。如果classinfo
是类型对象的元组(或递归的其他此类元组)或多个类型的联合类型,如果object是任何类型的实例则返回True。如果classinfo不是类型或类型的元组以及此类元组,则会引发TypeError
异常。
这个type 我用的也不多,以后再解释吧。
✅一点小知识:
isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。isinstance() 与 type() 区别:
1.type() 不会认为子类是一种父类类型,不考虑继承关系。
2.isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
✅示例:
class A:
pass
class B(A):
pass
print(isinstance(A(), A))
print(isinstance(B(), A))
print(type(A()) == A)
print(type(B()) == A)
后记
最近有个想法,每周、每月搞定一点小知识,比如学习python内置函数、了解微服务、写一个小工具、解读一个测试框架、测试平台之类的。