-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod.py
More file actions
76 lines (61 loc) · 1.47 KB
/
Copy pathmethod.py
File metadata and controls
76 lines (61 loc) · 1.47 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
class example:
def add(self, a, b):
x = a+b
return x
def add(self, a, b, c=0):
x = a+b+c
return x
obj = example()
print (obj.add(10,20))
print (obj.add(10,20,30))
#Using default argument
class Greet:
def hello(self, name = "Guest"):
print(f"Hello, {name}!")
g = Greet()
g.hello()
g.hello("Amit")
#Using Variable Arguments(*args)
class Calculator:
def multiply(self, *args):
result = 1
for num in args:
result *= num
print("Product is:", result)
c = Calculator()
c.multiply(2, 3)
c.multiply(2, 3, 4)
# #Using Keyword Argument
class Student:
def details(self, **kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
s = Student()
s.details(age=20, add="Kop", name="swap", id=106)
# #Polymorphism(Overriding)
class Animal:
def sound(self):
return "Some generic sound"
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
a = [Dog(), Cat(), Animal()]
for i in a:
print(i.sound())
class Vehicle:
def type(self, *args):
if len(args) == 1:
print(f"Vehicle Type: {args[0]}")
elif len(args) == 2:
print(f"Vehicle Type: {args[0]}, Brand: {args[1]}")
elif len(args) == 3:
print(f"Vehicle Type: {args[0]}, Brand:{args[1]}, Model:{args[2]}")
else:
print("Invalid number of details!")
v = Vehicle()
v.type("Car")
v.type("Bike", "Hero")
v.type("Truck", "Tata","2024")