Repository navigation
Expand file tree
/
Copy pathclasses_basics.py
More file actions
163 lines (116 loc) · 5.11 KB
/
Copy pathclasses_basics.py
File metadata and controls
163 lines (116 loc) · 5.11 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class house:
number_of_windows = 3
width = 5000
breadth = 2500
def __init__(self,color,window_size):
self.color = 'red'
self.roof_color = color
self.window_size = window_size
houses = [house("red","sm"), house("blue","lg"), house("green","red")]
print([[house.number_of_windows for house in houses]])
print([[house.roof_color for house in houses]])
print([[house.window_size for house in houses]])
# constructor and destructor
class house:
number_of_windows = 3
width = 5000
breadth = 2500
def __init__(self,color,window_size):
self.color = 'red'
self.roof_color = color
self.window_size = window_size
def __del__(self):
print('deleted', id(self))
houses = [house("red","sm"), house("blue","lg"), house("green","red")]
print([[house.number_of_windows for house in houses]])
print([[house.roof_color for house in houses]])
print([[house.window_size for house in houses]])
del houses[1]
# In Python, constructors and destructors are special methods that initialize and clean up objects, respectively. They are important concepts in object-oriented programming.
# Constructors Initialize an object's attributes with default or given values, Set up the object's initial state, Allocate resources, Defined with the init method, and Automatically called when an object is created.
# Destructors
# Perform cleanup actions like releasing resources, closing files, or freeing memory
# Clean up before an object is destroyed
# Defined with the __del__ method
# Automatically called when an object is about to be destroyed
# Differences between constructors and destructors
# Purpose: Constructors initialize objects, while destructors clean them up
# Invocation: Constructors are called when an object is created, while destructors are called when an object is deleted
# Resource management: Constructors acquire resources, while destructors release them
# Types of methods
# instance class and static
# In Python, the three main types of methods are instance, class, and static. Each type has different characteristics and is used in different situations.
# Instance methods: Can access both class and instance attributes, Receive the caller object as the first parameter, and Require no decorator.
# Class methods:
# Can access class attributes, but not instance attributes
# Receive the caller class as the first parameter
# Require the @classmethod decorator
# Used when the method needs to access or modify class-level attributes
# Static methods:
# Do not have access to class attributes or instance attributes
# Do not take any necessary parameter
# Require the @staticmethod decorator
# Used when the method's behavior does not depend on instance-specific or class-specific data
#instance method:
def print_house_info(houses):
print([[house.number_of_windows for house in houses]])
print([[house.roof_color for house in houses]])
print([[house.window_size for house in houses]])
print("------------------------")
class house:
number_of_windows = 3
width = 5000
breadth = 2500
def __init__(self,color,window_size):
self.color = 'red'
self.roof_color = color
self.window_size = window_size
def __del__(self):
print('deleted', id(self))
def do_paint_job(self, new_color):
self.color = new_color
houses = [house("red","sm"), house("blue","lg"), house("green","red")]
def print_house_info(houses):
print([[house.number_of_windows for house in houses]])
print([[house.roof_color for house in houses]])
print([[house.window_size for house in houses]])
print_house_info(houses)
houses[1].do_paint_job("maroon")
print_house_info(houses)
# static method
class House:
number_of_windows = 5
width = 5000
breadth = 2500
def __init__(self, color, window_size):
self.color = color
self.roof_color = "dark_red"
self.window_size = window_size
def __del__(self):
print("destroyed house", id(self))
def do_paint_job(self, new_color):
self.color = new_color
def get_data(self):
self.find_area(self.width, self.breadth )
return self.color, self.roof_color, self.window_size , self.find_area(self.width, self.breadth )
@staticmethod
def find_area(length, width):
return length * width
@classmethod
def red_house(cls, windows_size):
return cls("red", windows_size)
def __add__( self, other):
# Example function to print house information
def print_house_info(houses):
for index, house in enumerate(houses):
print(f"House {index + 1}: {house.get_data()}")
# Create house objects
houses = [House("red", "sm"), House("blue", "lg"), House("green", "md")]
# Print house information
print_house_info(houses)
# Access specific house data
print(houses[1].get_data())
# Calculate area using static method
area = House.find_area(100, 100)
print(f"Area: {area}")
print(houses[0] + houses[1])