-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_ternary.py
More file actions
47 lines (31 loc) · 1.21 KB
/
example_ternary.py
File metadata and controls
47 lines (31 loc) · 1.21 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
from ternary import ternary, ternary_call
""" Shorter:
There is an operator in C/C++ called ternary.
(checkSomething) ? do_this_if_something_was_true : do_this_if_it_was_false
in Python it goes like this 'do_this if something_true else do_something_else'
Short:
@ternary_call(func_when_result_true, func_when_result_false) automatically calls the function
in the decorator argument based on the decorated function return result.
@ternary(return_value_when_true, return_value_when_false) automatically returns the given
decorator argument based on the decorated function return result.
!Important make sure decorated function returns a BOOLEAN
"""
result_true = 12
result_false = -5
def func_true():
print("Result was True")
def func_false():
print("Result was False")
@ternary_call(func_true, func_false)
def some_function(result: bool):
return result
@ternary(result_true, result_false)
def some_other_function(result: bool):
return result
some_function(True)
some_function(True)
some_function(False)
print("\n\n\n")
print(some_other_function(True))
print(some_other_function(False))
print(some_other_function(False))