多一句没有,少一句不行,用最短的时间,教会你有用的技术。

all()

✅语法:

Return True if all elements of the iterable are true (or if the iterable is empty).

等价于下面这个:

  1. def all(iterable):
  2. for element in iterable:
  3. if not element:
  4. return False
  5. return True

✅ 解释:
如果iterable中的所有元素都为真(或者iterable为空),则返回True

✅示例:

  1. list1=[1,2,3,4,5,6]
  2. list2=[0,2,3,0,5,6]
  3. list3=[0,0,0,0,0,0]
  4. print(all(list1))
  5. print(all(list2))
  6. print(all(list3))
  7. # True
  8. # False
  9. # False

any()

✅语法:

Return True if any element of the iterable is true. If the iterable is empty, return False.

✅解释:

如果iterable中的任何元素为真,则返回真。如果可迭代对象为空,则返回False

✅示例:

  1. list1=[1,2,3,4,5,6]
  2. list2=[0,2,3,0,5,6]
  3. list3=[0,0,0,0,0,0]
  4. print(any(list1))
  5. print(any(list2))
  6. print(any(list3))
  7. # True
  8. # True
  9. # 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__()方法。

✅示例:

这个做算法题的时候会经常用到。

  1. print(bin(3))
  2. print(bin(-10))
  3. # 0b11
  4. # -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.

✅多说两句:

参考链接:

https://zhuanlan.zhihu.com/p/191419441

知道你看了上面的解释依然不会用。我们再来阐述一下:

一个可callable的对象是指可以被调用执行的对象,并且可以传入参数, 用另一个简单的描述方式,只要可以在一个对象的后面使用小括号来执行代码,那么这个对象就是callable对象,下面列举callable对象的种类:函数,类,类里的函数,实现了call方法的实例对象.

函数

  1. def test():
  2. print('ok')
  3. print(callable(test)) # True
  4. test() # ok

  1. class Stu(object):
  2. def __init__(self, name):
  3. self.name = name
  4. print(callable(Stu)) # True
  5. print(Stu('小明').name) # 小明

类里的方法

类里的方法也是用def定义的,本质上也是函数

  1. from inspect import isfunction, ismethod
  2. class Stu(object):
  3. def __init__(self, name):
  4. self.name = name
  5. def run(self):
  6. print('{name} is running'.format(name=self.name))
  7. print(isfunction(Stu.run)) # True
  8. stu = Stu("小明")
  9. stu.run() # 小明 is running
  10. 使用isfunction函数可以判断一个对象是否是函数,run方法也是可调用对象

实现了call方法的实例对象

  1. lass Stu(object):
  2. def __init__(self, name):
  3. self.name = name
  4. def __call__(self, *args, **kwargs):
  5. self.run()
  6. def run(self):
  7. print('{name} is running'.format(name=self.name))
  8. stu = Stu('小明')
  9. print(callable(stu)) # True
  10. 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.

内置函数1 - 图1

解释:

返回一个枚举对象。Iterable必须是序列、迭代器或其他支持迭代的对象。enumerate()返回的迭代器的next()方法返回一个元组,其中包含一个计数(从start开始,默认为0)和通过迭代iterable获得的值。

示例:

内置函数1 - 图2

id

这个就不多说了,以后用到详细介绍。
✅解释:
返回一个对象的“标识”。

isinstance & type

✅语法:

内置函数1 - 图3

isinstance:如果object参数是classinfo参数的实例,或其(直接、间接或虚)子类的实例,则返回True。如果object不是给定类型的对象,函数总是返回False。如果classinfo是类型对象的元组(或递归的其他此类元组)或多个类型的联合类型,如果object是任何类型的实例则返回True。如果classinfo不是类型或类型的元组以及此类元组,则会引发TypeError异常。

这个type 我用的也不多,以后再解释吧。

内置函数1 - 图4

✅一点小知识:

isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type()。isinstance() 与 type() 区别:

1.type() 不会认为子类是一种父类类型,不考虑继承关系。
2.isinstance() 会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同推荐使用 isinstance()。

✅示例:

  1. class A:
  2. pass
  3. class B(A):
  4. pass
  5. print(isinstance(A(), A))
  6. print(isinstance(B(), A))
  7. print(type(A()) == A)
  8. print(type(B()) == A)

后记

最近有个想法,每周、每月搞定一点小知识,比如学习python内置函数、了解微服务、写一个小工具、解读一个测试框架、测试平台之类的。