美文网首页
python(十一) 常用c、d 开头的内置函数

python(十一) 常用c、d 开头的内置函数

作者: 淡漠不淡漠 | 来源:发表于2019-11-25 20:01 被阅读0次

    callable() 函数用于检查一个对象是否是可调用的

    • 返回:True,object 仍然可能调用失败
    • 返回: False,调用对象 object 绝对不会成功
    #!/usr/bin/python3
    
    def add(a, b):
        return a+b
    
    print('函数add是否可以被调用:', callable(add))
    #输出结果:函数add是否可以被调用: True
    

    chr() 返回数字对应的字符

    • 参数为0-255的整数,可以是十进制,也可以十六进制
    #!/usr/bin/python3
    print('a的十进制是97:', chr(97))
    print('a的十六进制是0x61:', chr(0x61))
    #输出结果:
    a的十进制是97: a
    a的十六进制是0x61: a
    

    complex() 转化为复数

    #!/usr/bin/python3
    print(complex(1))
    print(complex('1+3j'))
    #输出结果:
    (1+0j)
    (1+3j)
    

    delattr(Object, attrName) 删除对象中的属性

    • 参数Object 对象
    • attrName 属性名
    #!/usr/bin/python3
    class My:
        a = 1
        b = 2
        c = 3
    m = My()
    print(m.a)
    #输出结果:1
    delattr(My, 'a')  # 删除 a属性
    #输出结果:AttributeError: 'My' object has no attribute 'a'
    

    dict()创建字典

    • 有以下3种方式
    #!/usr/bin/python3
    d1 = dict(a='1', b='2', c='3')
    print('d1=', d1)
    
    d2 = dict(zip(['a','b','c'], ['1', '2', '3']))
    print('d2=', d2)
    
    d3 = dict([('a','1'), ('b', '2'), ('c', '3')])
    print('d3=', d3)
    
    #输出结果:
    d1= {'a': '1', 'b': '2', 'c': '3'}
    d2= {'a': '1', 'b': '2', 'c': '3'}
    d3= {'a': '1', 'b': '2', 'c': '3'}
    

    dir() 查看属性方法

    • 不传值默认返回
    #!/usr/bin/python3
    print('dir不带参数默认值:', dir())
    # 输出结果:
    dir不带参数默认值: ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
    
    • 传入一个空列表
    #!/usr/bin/python3
    print('dir传入一个空列表:', dir([ ]))
    
    #输出结果:
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    
    • 传入一个空字典
    #!/usr/bin/python3
    print('dir传入一个空字典:', dir({ }))
    #输出结果:
    ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
    

    divmod() 可以将除法得到商和余数

    print('7除以2:', divmod(7, 2))
    #输出结果:
    7除以2: (3, 1)
    

    相关文章

      网友评论

          本文标题:python(十一) 常用c、d 开头的内置函数

          本文链接:https://www.haomeiwen.com/subject/bbgjwctx.html