-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_3.py
More file actions
38 lines (24 loc) · 991 Bytes
/
module_3.py
File metadata and controls
38 lines (24 loc) · 991 Bytes
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
# Stepick.org — Углублённый Python
# 3. Утиная типизация и контракты в Python
from typing import Protocol, runtime_checkable
import random
# 3.3 typing.Protocol
@runtime_checkable
class PaymentProtocol(Protocol):
transaction_id: int
def process_payment(self, amount: float) -> str: ...
class CreditCard:
def __init__(self):
self.transaction_id = random.randint(1000, 9999)
def process_payment(self, amount: float) -> str:
return "Оплата успешно проведена."
class PayPal:
def __init__(self):
self.transaction_id = random.randint(1000, 9999)
def process_payment(self, amount: float) -> str:
return "Оплата успешно проведена."
def process_transaction(payment: PaymentProtocol, sum: float):
if isinstance(payment, PaymentProtocol):
result = payment.process_payment(sum)
print(result)
print(payment.transaction_id)