-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass_Object_Constructor_Method.py
More file actions
44 lines (34 loc) · 1.3 KB
/
Class_Object_Constructor_Method.py
File metadata and controls
44 lines (34 loc) · 1.3 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
# Class Object Constructor Method
from ast import Return
# Class definition
class book:
# Class Variables. Changing here will impact all objects.
booktype = "Hardcover"
# Constructor __init__
def __init__(self, name, price, genre):
# Instance Variables
self.name = name # self.name is storing the name for the object being created
self.price = price # self.price is storing the price for the object being created
self.genre = genre # self.genre is storing the genre for the object being created
# Function within class or Method
def compare(self,valueFromOther) :
if (self.price == valueFromOther.price) :
return True
else :
return False
book1 = book("1984", 199, "Dystopian") # book1 is the object of the class book and Passing the value.
book2 = book("Animal Farm", 199, "Political Satire") # book2 is the object of the class book and Passing the value.
print(book1.name)
print(book1.price)
print(book1.genre)
print(book1.booktype)
print(book2.name)
print(book2.price)
print(book2.genre)
print(book1.booktype)
# Checking if both books has same price
check = book1.compare(book2)
if check == True :
print("Same Price")
else :
print("Different Price")