python built-in

# -*- coding:utf-8 -*-
'''
Created on 2012-8-19
@author: corleone
'''
import random

def test_all_1():
    '''
    @summary: test all  
    '''
    def print_x(x):
        print x
        return x
    
    print u"get all member first , then calc" , all([print_x(i) == 0  for i in range(10)])
    print u"get one member and calc , then get next " , all((print_x(i) == 0  for i in range(10)))

def test_any_1():
    '''
    @summary: like all
    '''

def test_bin_1():
    print bin(3)

def test_int_1():
    '''
    int([x[, base]]) 
    @attention: base in [2,36]
    '''
    print 2, int(u"0b11", 2)
    print 8, int(u"11", 8)
    print 16, int(u"11", 16)
    print 5, int(u"11", 5)
    print 36, int(u"11", 36)
#    print 37, int(u"11", 37)

def test_chr_1():
    print chr(97), type(chr(97))

def test_unichr_1():
    print unichr(97), type(unichr(97))

def test_ord_1():
    print ord("a")

def test_hex_1():
    print hex(16)
    
def test_oct_1():
    print oct(17)

def test_map_1():
    print map(lambda x:x * 2, range(4))
    print map(lambda x, y:unicode(x) + unicode(y), range(4), range(3, 6))
    print map(lambda x, y, z:u"+".join(map(unicode, [x, y, z])), range(4), range(3, 7), range(7, 9))

def test_max_1():
    items = range(10)
    random.shuffle(items)
    print items
    print max(items)
    print max(*items)

def test_iter_1():
    '''
    @summary: iter(o[, sentinel]) 
    @attention: 
    Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then o must be a callable object. The iterator created in this case will call o with no arguments for each call to its next() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.
One useful application of the second form of iter() is to read lines of a file until a certain line is reached. The following example reads a file until "STOP" is reached:
with open("mydata.txt") as fp:
    for line in iter(fp.readline, "STOP"):
        process_line(line)
New in version 2.2.
    @param sentinel: 哨兵
    '''
    for i in iter(range(10)):
        print i 
    
    with open(u"built_in.py") as f:
        for i in iter(f.readline, u"""    with open(u"built_in.py") as f:\n"""):
            print i
        
def test_next_1():
    '''
    @summary: 
    Retrieve the next item from the iterator by calling its next() method. 
    If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.
    '''
    items = iter(range(10))
    while 1:
        print next(items)
#    while 1:
#        print next(items, 100) # loop will not stop 

def test_pow_1():
    '''
    @summary: pow(x, y[, z]) 
    Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z).
    The two-argument form pow(x, y) is equivalent to using the power operator: x**y.
            此方法是快速指数取模的优化实现,可以演算,已推算过,超管用
    '''
    print pow(2,5,3)
    


if __name__ == '__main__':
    test_all_1() 
    test_bin_1()
    test_int_1()
    test_chr_1()
    test_ord_1()
    test_unichr_1()
    test_hex_1()
    test_oct_1()
    test_map_1()
    test_max_1()
    test_iter_1()
    test_next_1()
    test_pow_1()



看python doc 的 built-in 部分 。 选了些比较实用的记下来。

你可能感兴趣的:(python built-in)