-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance_methods.py
More file actions
75 lines (58 loc) · 2.73 KB
/
Copy pathinstance_methods.py
File metadata and controls
75 lines (58 loc) · 2.73 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
# 1. class and object
# create car class with attributes(variables) like brand and model. then create an instance of that class
class Car:
total_cars = 0
#default constructor
def __init__(self, brand, model): #self refers to whoever calls the function/context, __init__ function is known as a constructor
self.__brand = brand #self.brand refers to the variables/instances within the class, whereas brand model refers to paramaters passed by the user
#brand is privated using encapsulation
self.model = model
Car.total_car += 1
# 4. Encapsulation
# modify car class to encapsulate the brand attribute, making it private and provide a getter method for it
#paramaterised constructor
def get_brand(self):
return self.__brand + " !"
# 2. class method and self
# add a method to car which displays the full name of the car the model and the name
def full_name(self):
return f"{self.__brand}{self.model}" #formatted string
# 5. Polymorphism
# demonstrate polymorphism by defining a method fuel_type in both Car and ElectricCar classes with different behaviour
def fuel_type(self): # method - functions that belong to objects
return "Petrol and Diesel"
# 3. Inheritance
# create an electric car class that oinherits from car class and has an additional attribute battery_size
class ElectricCar(Car): # inherits proprties of car class ie brand and model
def __init__(self, brand, model, battery_size):
super().__init__(brand, model)
self.battery_size = battery_size
def fuel_type(self):
return "Electric Charge"
my_tesla = ElectricCar("tesla", "model s", "85kWH")
print(my_tesla.model)
print(my_tesla.get_brand())
safari = Car("tata", "safari")
print(safari.fuel_type())
my_car = Car("toyota", "corolla") # object
print(my_car.__brand)
print(my_car.model)
my_new_car = Car("tata", "safari")
print(my_new_car.model) # class defination hence no paranthesis needed
# generalised form, usually the class is created in a seperate file imported whenever it is to be used
print(my_new_car.full_name()) # defined functions adds functionality, hence paranthesis should be added to the function call
#types of attributes
# 1. class attributes: owned by the class, common for all in class
# 2. instance attributes: different for each object
#property decorator - is used in the class to use the method as a property
class Student:
def __init__(self,phy,chem,math):
self.phy = phy
self.chem = chem
self.math = math
# self.percentage = str((self.phy+self.chem+self.math)/3) + "%"
@property #if any change in properties hence this will change accordingly
def percentage(self):
return str((self.phy+self.chem+self.math)/3) + "%"
stu = Student(98,67,69)
print(stu.percentage)