python基础模块---functools

import functools

'''
functools.cmp_to_key(func)
    将老式的比较函数(func)转换为关键字函数(key function)。在 Python3中比较大小、排序都是基于关键字函数的,
    Python 3 不支持老式的比较函数。
'''

class Person(object):

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f"My name is {self.name}"


def compare(p1, p2):
    return len(p1.name) - len(p2.name)


data = [Person('张三'), Person('赵飞燕'), Person('汉武大帝'), Person('松下幸之助'), Person('爱新觉罗玄烨'), Person('阿尔伯特爱因斯坦')]
data.sort(key=functools.cmp_to_key(compare))
print(data)
# [My name is 张三, My name is 赵飞燕, My name is 汉武大帝, My name is 松下幸之助, My name is 爱新觉罗玄烨, My name is 阿尔伯特爱因斯坦]


'''
functools.reduce(function, iterable[, initializer])
    将初始值(默认为 0,可由 initializer 参数指定)、迭代器的当前元素传入 function 函数,将计算出来的函数结果作为下一次计算的初始值、
迭代器的下一个元素再次调用 function 函数……依此类推,直到迭代器的最后一个元素。
'''
print(functools.reduce(lambda x, y: x + y, range(10), 100))

'''
@functools.lru_cache(maxsize=128, typed=False)
    该函数装饰器使用 LRU(最近最少使用)缓存算法来缓存相对耗时的函数结果,避免传入相同的参数重复计算。同时,缓存并不会无限增长,
不用的缓存会被释放。其中 maxsize 参数用于设置缓存占用的最大字节数,typed 参数用于设置将不同类型的缓存结果分开存放。
'''


@functools.lru_cache(maxsize=128)
def fibnacci(n):
    if n in (1, 2):
        return 1
    else:
        return fibnacci(n - 1) + fibnacci(n - 2)


print(fibnacci(100))


'''
functools.partial(func, *args, **keywords)
    该函数用于为func函数的部分参数指定参数值,从而得到一个转换后的函数,程序以后调用转换后的函数时,就可以少传入那些己指定值的参数。
'''


def add(x, y):
    return x + y


par_func = functools.partial(add, 10)
print(par_func(5))  # 15


'''
partialmethod() 与 partial() 函数的作用基本相似,区别只是 partial() 函数用于为函数的部分参数绑定值;
而 partialmethod() 函数则用于为类中方法的部分参数绑定值。如下程序示范了 partialmethod() 函数的用法:
'''

class Person(object):

    def __init__(self):
        self._age = 18

    @property
    def age(self):
        return self._age

    def set_age(self, number: int):
        if not isinstance(number, int):
            raise ValueError('int needed')
        if number > 60:
            self._age = 100
        elif number < 10:
            self._age = 5
        else:
            self._age = 18

    set_young = functools.partialmethod(set_age, 8)
    set_old = functools.partialmethod(set_age, 70)


p = Person()
print(p.age)        # 18
p.set_young()       # 相当于调用set_age(8)
print(p.age)        # 5
p.set_old()         # 相当于调用set_age(70)
print(p.age)        # 100


'''
@functools.total_ordering
    这个类装饰器(作用类似于函数装饰器,只是它用于修饰类)用于为类自动生成比较方法。通常来说,开发者只要提供 
__lt__()、__le__()、__gt__()、__ge__() 其中之一(最好能提供 __eq__() 方法),@functools.total_ordering装饰器就会
为该类生成剩下的比较方法。
'''

@functools.total_ordering
class Person(object):

    def __init__(self, age):
        self.age = age

    def __eq__(self, other):
        return self.age == other.age

    def __gt__(self, other):
        if self.age > other.age:
            return True
        return False


p1 = Person(18)
p2 = Person(20)
print(p1 > p2)      # False
print(p1 >= p2)     # False
print(p1 < p2)      # True
print(p1 <= p2)     # True
print(p1 == p2)     # False


'''
@functools.singledispatch
    该函数装饰器用于实现函数对多个类型进行重载。比如同样的函数名称,为不同的参数类型提供不同的功能实现.
    该函数的本质就是根据参数类型的变换,将函数转向调用不同的函数。
'''


@functools.singledispatch
def test(args):
    print(args)


@test.register(int)
def test_int(args):
    print('the args is int')


@test.register(float)
def test_float(args):
    print('the args is float')


@test.register(list)
def test_list(args):
    print('the args is list')


@test.register(dict)
@test.register(set)
def test_dict_set(args):
    print('the args is dict or set')


test(1)     # the args is int
test(1.0)       #the args is float
test(list(range(5)))  # the args is list
test(dict([(1, 2), (3, 4)]))  # the args is dict or set
print(test.registry.keys())  # 获取test函数所绑定的全部类型
print(test.registry[int])  # 获取test函数绑定的int类型的函数

'''
@functools.wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
    该函数装饰器用于修饰包装函数,使包装函数看上去就像 wrapped 函数。
'''


def decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        return result

    return wrapper


@decorator
def test():
    """我是test函数"""
    print('test is calling')


test()  # test is calling
print(test.__name__)  # test,不加装饰器会显示为wrapper
print(test.__doc__)  # 我是test函数,不加装饰器得不到说明文档


'''
    functools.update_wrapper和functools.wraps的区别在于,functools.update_wrapper是一个函数,它的第一个参数是包装函数,第二个参数是
被包装函数。
'''
def decorator(func):

    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        functools.update_wrapper(wrapper, func)
        return result

    return wrapper

@decorator
def test():
    """我是test函数"""
    print('test is calling')

test()
print(test.__name__)
print(test.__doc__)

你可能感兴趣的:(python基础模块---functools)