首页 > 代码库 > python __builtin__模块介绍

python __builtin__模块介绍

# encoding: utf-8
# module __builtin__
# from (built-in)
# by generator 1.143

from __future__ import print_function
"""
Built-in functions, exceptions, and other objects.

Noteworthy: None is the `nil‘ object; Ellipsis represents `...‘ in slices.
"""

# imports
from exceptions import (ArithmeticError, AssertionError, AttributeError, 
    BaseException, BufferError, BytesWarning, DeprecationWarning, EOFError, 
    EnvironmentError, Exception, FloatingPointError, FutureWarning, 
    GeneratorExit, IOError, ImportError, ImportWarning, IndentationError, 
    IndexError, KeyError, KeyboardInterrupt, LookupError, MemoryError, 
    NameError, NotImplementedError, OSError, OverflowError, 
    PendingDeprecationWarning, ReferenceError, RuntimeError, RuntimeWarning, 
    StandardError, StopIteration, SyntaxError, SyntaxWarning, SystemError, 
    SystemExit, TabError, TypeError, UnboundLocalError, UnicodeDecodeError, 
    UnicodeEncodeError, UnicodeError, UnicodeTranslateError, UnicodeWarning, 
    UserWarning, ValueError, Warning, WindowsError, ZeroDivisionError)

abs(number)  #返回值是一个数字的绝对值,如果是复数,返回值是复数的模

abs(-1.2) #返回 1.2
abs(1.2) #返回 1.2
abs(-1-1j) #返回 1.41421356237

all(iterable)  #所有的值为真时才为真,只要有一个是假就是假

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True
>>> all([a, b, c, d])  #列表list,元素都不为空或0
True
>>> all([a, b, ‘‘, d])  #列表list,存在一个为空的元素
False
>>> all([0, 1,2, 3])  #列表list,存在一个为0的元素
False 
>>> all((a, b, c, d))  #元组tuple,元素都不为空或0
True
>>> all((a, b, ‘‘, d))  #元组tuple,存在一个为空的元素
False
>>> all((0, 1,2, 3))  #元组tuple,存在一个为0的元素
False
>>> all([]) # 空列表
True
>>> all(()) # 空元组
True

any(iterable)  #只要有一个为真就是真

def any(iterable):
   for element in iterable:
       if  element:
           return False
   return True
>>> any([a, b, c, d])  #列表list,元素都不为空或0
True
>>> any([a, b, ‘‘, d])  #列表list,存在一个为空的元素
True
>>> any([0, ‘‘, False])  #列表list,元素全为0,‘‘,false
False
>>> any((a, b, c, d))  #元组tuple,元素都不为空或0
True
>>> any((a, b, ‘‘, d))  #元组tuple,存在一个为空的元素
True
>>> any((0, ‘‘, False))  #元组tuple,元素全为0,‘‘,false
False 
>>> any([]) # 空列表
False
>>> any(()) # 空元组
False

apply(p_object, args=None, kwargs=None)  

# 用于当函数参数已经存在于一个元组或字典中时,间接地调用函数。args是一个包含将要提供给函数的按位置传递的参数的元组。如果省略了args,任何参数都不会被传递,kwargs是一个包含关键字参数的字典。
# apply()的返回值就是func()的返回值,apply()的元素参数是有序的,元素的顺序必须和func()形式参数的顺序一致

# 没有带参数
def say():
    print say in
apply(say)
# 只带元组的参数  
def say(a, b):
    print a, b
apply(say,("hello", "张三python"))  # function(object, *args)
# 带关键字参数
def say(a=1,b=2):
    print a,b
def haha(**kw):
    apply(say,(),kw)   # function(object, (), **keywords)
print haha(a=a,b=b)

bin(number)  #将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer

#整数的情况, 前面多了0b,这是表示二进制的意思。
bin(521)
0b1000001001
#非整型的情况,必须包含__index__()方法切返回值为integer的类型
class Type:
    def __index__(self):
        return 8
testbin = Type()
print bin(testbin)
0b1000

callable(p_object)  #检查对象object是否可调用。如果返回True,object仍然可能调用失败;但如果返回False,调用对象ojbect绝对不会成功。

>>> class A:
    def add(a, b):
        return a + b
>>> callable(A)
True
>>> a = A()
>>> callable(a)
False
>>> class B:
    def __call__(self):
        return 0
>>> callable(B)
True
>>> b = B()
>>> callable(b)  #类是可调用的,而类的实例实现了__call__()方法才可调用
True

chr(i)  # 返回整数i对应的ASCII字符,i取值范围[0, 255]之间的正数

>>> chr(27)
\x1b
>>> chr(97)
a
>>> chr(98)
b

 

python __builtin__模块介绍