-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.py
33 lines (24 loc) · 1.18 KB
/
class.py
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
""""
if we dont use __init__ constructor then we have to manually defien the attribute of the class as either blank variable in the class definition
And
Also need to pass the values separatelty at the time of calling the class as shown below:
OR we need to hard-code the value of color and flavor in the class which is least-recommended approach.
jonagold.color = "red"
jonagold.flavor = "sweet"
Constructor makes classes more dynamic by avoiding hard-coding of attributes and also avoids repeated attributes definition at the time of calling the class
""""
class Apple:
def __init__(self, color, flavor): # __init__ is a spcial method called constructor of a classa and it is called initially when the class is instantiaed
self.color = color
self.flavor = flavor
def __str__(self): # __str__ method ensures to avoid printing the memory address and instead print below message when the instance of this class is passed to print()
return "The Apple is {} in color and {} " . format(self.color, self.flavor)
jonagold = Apple("red", "sweet")
print(jonagold.color)
print(jonagold.flavor)
print(jonagold)
OutPut:
-------
red
sweet
The Apple is red in color and sweet