-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctools.py
More file actions
59 lines (43 loc) · 1.36 KB
/
functools.py
File metadata and controls
59 lines (43 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from functools import reduce, singledispatch
# A number of tools in Python accept key functions to control how elements are ordered or grouped. They include min(), max(), sorted(), list.sort(), heapq.merge(), heapq.nsmallest(), heapq.nlargest(), and itertools.groupby().
# mm = functools.cmp_to_key()
#
# sorted([111,12,12,134,506,78], key=cmp_to_key(lambda x, y: y-x))
# @lru_cache
# def fib(n):
# if n < 2:
# return n
# return fib(n-1) + fib(n-2)
# @total_ordering
# basetwo = partial(int, base=2)
# print basetwo
# class Cell(object):
# def __init__(self):
# self._alive = False
# @property
# def alive(self):
# return self._alive
# def set_state(self, state):
# self._alive = bool(state)
# set_alive = partialmethod(set_state, True) set_dead = partialmethod(set_state, False)
# mm = reduce(lambda x, y: x+y, [1,2,3,4,5])
# print mm
@singledispatch
def func(arg, verbose=Flase):
if verbose:
print 'let me say,', end=""
print arg
@fun.register(int)
fun.register(type(None), nothing)
fun.registry.keys()
from functools import wraps
def my_decorator(f):
@wraps(f)
def wrapper(*args, **kwds):
print('Calling decorated function')
return f(*args, **kwds)
return wrapper
@my_decorator
def example():
"""Docstring"""
print('Called example function')