-
Notifications
You must be signed in to change notification settings - Fork 2
/
function_addition.py
62 lines (46 loc) · 1.81 KB
/
function_addition.py
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
57
58
59
60
61
62
"""
I often find that I end up needing to write a function to return multiple values, in this case I would often split it up
into two different functions but then I have to spend time thinking of a new function name! Wouldn't it be great if I
could use same name for a function again and again...
In this kata your task is to make a decorator, FuncAdd which will allow function names to be reused and when called all
functions with that name will be called (in order) and the results returned as a tuple.
@FuncAdd
def foo():
return 'Hello'
@FuncAdd
def foo():
return 'World'
foo() --> ('Hello', 'World')
As well as this you will have to implement two more things, a way of deleting a particular function, and a way of
deleting all stored functions. The delete method must work as follows:
@FuncAdd
def foo():
return 'Hello'
FuncAdd.delete(foo) # Delete all foo() functions only
foo() # Should raise NameError
And the clear method must work like this:
@FuncAdd
def foo():
return 'Hello'
FuncAdd.clear() # Delete all decorated functions
foo() # Should raise NameError
The functions must also accept args and kwargs which would all be passed to every function.
"""
class FuncAdd:
functions = {}
def __init__(self, func):
self.name = func.__name__
if self.name not in FuncAdd.functions.keys():
FuncAdd.functions[self.name] = []
FuncAdd.functions[self.name].append(func)
def __call__(self, *args, **kwargs):
if self.name not in FuncAdd.functions.keys():
raise NameError
return tuple([func(*args, **kwargs) for func in FuncAdd.functions[self.name]])
@classmethod
def delete(cls, func):
if func.name in FuncAdd.functions.keys():
del cls.functions[func.name]
@classmethod
def clear(cls):
cls.functions = {}