Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> sum = lambda x, y : x + y
- >>> sum(1, 2)
- 3
- >>> incr = lambda y : sum(1, y)
- >>> incr(2)
- 3
- >>> def sum2(x, y):
- return x + y
- >>> incr2 = functools.partial(sum2, 1)
- >>> incr2(4)
- 5
- incr = lambda y : sum(1, y)
- def partial(func, *part_args):
- def wrapper(*extra_args):
- args = list(part_args)
- args.extend(extra_args)
- return func(*args)
- return wrapper
- class EventNotifier(object):
- def __init__(self):
- self._listeners = []
- def add_listener(self, callback):
- ''' callback should accept two positional arguments, event and params '''
- self._listeners.append(callback)
- # ...
- def notify(self, event, *params):
- for f in self._listeners:
- f(event, params)
- def log_event(context, event, params):
- context.log_event("Something happened %s, %s", event, params)
- class Listener(object):
- def __init__(self, context):
- self._context = context
- def __call__(self, event, params):
- self._context.log_event("Something happened %s, %s", event, params)
- notifier.add_listener(Listener(context))
- log_listener = lambda event, params: log_event(context, event, params)
- notifier.add_listener(log_listener)
- context = get_context() # whatever
- notifier.add_listener(partial(log_event, context))
- # create some data
- import random as RND
- fnx = lambda: RND.randint(0, 10)
- data = [ (fnx(), fnx()) for c in range(10) ]
- target = (2, 4)
- import math
- def euclid_dist(v1, v2):
- x1, y1 = v1
- x2, y2 = v2
- return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
- data.sort(key=euclid_dist)
- from functools import partial
- p_euclid_dist = partial(euclid_dist, target)
- >>> p_euclid_dist((3, 3))
- 1.4142135623730951
- data.sort(key=p_euclid_dist)
- # verify that it works:
- for p in data:
- print(round(p_euclid_dist(p), 3))
- 1.0
- 2.236
- 2.236
- 3.606
- 4.243
- 5.0
- 5.831
- 6.325
- 7.071
- 8.602
- >>> from functools import partial
- >>> def fnx(a, b, c):
- return a + b + c
- >>> fnx(3, 4, 5)
- 12
- >>> pfnx = partial(fnx, a=12)
- >>> pfnx(b=4, c=5)
- 21
- >>> pfnx = partial(fnx, 12)
- >>> pfnx(4, 5)
- 21
- >>> pfnx = partial(fnx, a=12)
- >>> pfnx(4, 5)
- Traceback (most recent call last):
- File "<pyshell#80>", line 1, in <module>
- pfnx(4, 5)
- TypeError: fnx() got multiple values for keyword argument 'a'
- >>> import multiprocessing as MP
- >>> # create a process pool:
- >>> ppool = MP.Pool()
- >>> ppool.map(pfnx, [4, 6, 7, 8])
- search(pattern, string, flags=0)
- is_spaced_apart = partial(re.search, '[a-zA-Z]s=')
- is_grouped_together = partial(re.search, '[a-zA-Z]=')
- is_spaced_apart(string, flags=0) # pattern '[a-zA-Z]s=' applied
- is_grouped_together(string, flags=0) # pattern '[a-zA-Z]=' applied
- for text in lines:
- if is_grouped_together(text):
- some_action(text)
- elif is_spaced_apart(text):
- some_other_action(text)
- else:
- some_default_action()
- from functools import partial
- def foo(a,b):
- return a+b
- bar = partial(foo, a=1) # equivalent to: foo(a=1, b)
- bar(b=10)
- #11 = 1+10
- bar(a=101, b=10)
- #111=101+10
- from functools import partial
- def add(a,b):
- return a + b
- def add2number(x,y,z):
- return x + y + z
- if __name__ == "__main__":
- add2 = partial(add,2)
- print("result of add2 ",add2(1))
- add3 = partial(partial(add2number,1),2)
- print("result of add3",add3(1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement