-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic function
123 lines (86 loc) · 3.54 KB
/
magic function
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class Customer:
def __init__(self, name, balance=5):
self.name = name
self.balance = balance
print("The init method was called")
def __str__(self):
return 'Customer: ' + str(self.name) + ', balance: ' + str(self.balance)
def __repr__(self):
return f"Customer(name={self.name}, balance={self.balance})"
# # Equality check
def __eq__(self, other):
return self.balance == other.balance
# # Less than comparison
def __lt__(self, other):
return self.balance < other.balance
# # Less than or equal to comparison
def __le__(self, other):
return self.balance <= other.balance
# # Greater than comparison
def __gt__(self, other):
return self.balance > other.balance
# # Greater than or equal to comparison (already provided in the original)
def __ge__(self, other):
return self.balance >= other.balance
# # Addition of balances between two Customer objects
def __add__(self, other):
return Customer(self.name + ' & ' + other.name, self.balance + other.balance)
# # Subtraction of balances between two Customer objects
def __sub__(self, other):
return Customer(self.name + ' & ' + other.name, self.balance - other.balance)
# # Creating Customer instances
customer1 = Customer("Alice", 10)
customer2 = Customer("Bob", 7)
customer3 = Customer("Charlie", 10)
# # Magic Method Examples
# # __str__ : String conversion
print(str(customer1)) # Customer: Alice, balance: 10
# # __repr__ : Developer-friendly string conversion
print(repr(customer2)) # Customer(name=Bob, balance=7)
# # __eq__ : Equality comparison
print(customer1 == customer2) # False (10 != 7)
print(customer1 == customer3) # True (10 == 10)
# # __lt__ : Less than comparison
print(customer2 < customer1) # True (7 < 10)
print(customer1 < customer3) # False (10 is not less than 10)
# # __le__ : Less than or equal to comparison
print(customer2 <= customer1) # True (7 <= 10)
print(customer1 <= customer3) # True (10 <= 10)
# # __gt__ : Greater than comparison
print(customer1 > customer2) # True (10 > 7)
print(customer2 > customer3) # False (7 is not greater than 10)
# # __ge__ : Greater than or equal to comparison
print(customer1 >= customer2) # True (10 >= 7)
print(customer1 >= customer3) # True (10 >= 10)
# # __add__ : Adding balances
customer4 = customer1 + customer2 # Adds balances and concatenates names
print(customer4) # Customer: Alice & Bob, balance: 17
# # __sub__ : Subtracting balances
customer5 = customer1 - customer2 # Subtracts balances and concatenates names
print(customer5) # Customer: Alice & Bob, balance: 3
# class Customer:
# def __init__(self, name, balance=20):
self.name = name
self.balance = balance
print("The init method was called")
# def __add__(self, other):
return Customer("Test_Customer",self.balance + other)
def __sub__(self, other):
return Customer("Test_Customer",self.balance - other)
c1 = Customer("Ali")
print(c1.balance)
print(str(c1))
c2 = c1 + 30
c3 = c1 - 3
print(c2.balance , c3.balance)
###Constructors Vs. Destructors
class Employee:
def __init__(self , balance = 15):
self.balance = balance
print('Employee created.')
def __add__(self , other):
return Employee(self.balance - other.balance)
def __del__(self):
print('Destructor called, Employee deleted.')
obj1 = Employee()
del obj1