Skip to content

Commit 2b155ce

Browse files
Create Positive_Negative_in_a_creative_way.py
1 parent e321fcd commit 2b155ce

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class ClassificationContext:
2+
def __init__(self, number):
3+
self.number = number
4+
5+
def __enter__(self):
6+
return self
7+
8+
def __exit__(self, exc_type, exc_value, traceback):
9+
pass
10+
11+
def positive(fn):
12+
def wrapper(context):
13+
return fn(context) if context.number > 0 else None
14+
return wrapper
15+
16+
def negative(fn):
17+
def wrapper(context):
18+
return fn(context) if context.number < 0 else None
19+
return wrapper
20+
21+
def zero(fn):
22+
def wrapper(context):
23+
return fn(context) if context.number == 0 else None
24+
return wrapper
25+
26+
num = float(input("Enter a number: "))
27+
28+
with ClassificationContext(num) as context:
29+
classification = (
30+
positive(lambda c: "positive"),
31+
negative(lambda c: "negative"),
32+
zero(lambda c: "zero")
33+
)
34+
35+
result = next((classify(context) for condition, classify in classification if condition(context)), None)
36+
37+
print(f"The number is {result}.")

0 commit comments

Comments
 (0)