Python随笔之内置函数

内置函数

Build-in Functions
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
delattr() hash() memoryview() set()
详细见python文档,猛击这里
  • abs() 参数为数字类型 获取绝对值
  • all() 参数为可迭代对象 循环对象,全部为True,那么方法返回为True
  • any() 参数为可迭代对象 循环对象,只要有一个为True,那么方法返回 为True
  • ascii() 参数为对象,获取该对象类中的__repr__方法的返回值
class Foo():
    def __repr__(self):
        return 'foo'
class Bar():
    pass

print(ascii(Foo()))
print(ascii(Bar()))
>>> foo
>>> <__main__.Bar object at 0x0162CE70>
  • bin() 二进制
  • oct() 八进制
  • hex() 十六进制
  • int() 十进制
i = 15
print(bin(i))
print(oct(i))
print(hex(i))
print(int(i))

>>> 0b1111
>>> 0o17
>>> 0xf
>>> 15
  • ord() 获取字符的整数表示

  • chr() 把编码转换为字符
    乱码问题

  • callable() 参数为一个对象,表示对象是否可执行

def foo():
    pass

i = 10

class Bar():
    def __call__(self, *args, **kwargs):
        pass

print(callable(foo))
print(callable(i))
bar = Bar()
print(callable(bar))

>>> True
>>> False
>>> True
posted @ 2019-02-17 20:21  新科鲁兹  阅读(119)  评论(0)    收藏  举报