-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path43_Functools_module.py
More file actions
48 lines (34 loc) · 1.14 KB
/
43_Functools_module.py
File metadata and controls
48 lines (34 loc) · 1.14 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
"""The functools module offers a collection of tools that simplify working with functions and callable objects."""
# 1. Partial class
"""The partial class fix certain arguments of a function and create a new function with fewer parameters."""
# Example:
from functools import partial
def power(a, b):
return a ** b
pow2 = partial(power, b=2)
pow4 = partial(power, b=4)
power_of_5 = partial(power, 5)
print(power(2, 3))
print(pow2(4))
print(pow4(3))
print(power_of_5(2))
print(pow2.func)
print(pow2.keywords)
print(power_of_5.args)
# 2. Partialmethod Class
"""Partialmethod works like partial, but for class methods.
It allows you to fix some method arguments when defining methods inside classes without making a new method manually."""
# Example:
from functools import partialmethod
class Demo:
def __init__(self):
self.color = 'black'
def _color(self, type):
self.color = type
set_red = partialmethod(_color, type='red')
set_blue = partialmethod(_color, type='blue')
set_green = partialmethod(_color, type='green')
obj = Demo()
print(obj.color)
obj.set_blue()
print(obj.color)