-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_10_Inheritance.py
More file actions
64 lines (57 loc) · 1.42 KB
/
Copy pathday_10_Inheritance.py
File metadata and controls
64 lines (57 loc) · 1.42 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
class Animal:
def speak(self):
print("Animal sound")
class Dog(Animal):
def bark(self):
print("Woof")
d=Dog()
d.speak()
d.bark()
#Polymorphism
class Dog:
def sound(self):
print("Woof")
class Cat:
def sound(self):
print("Meow")
#both have sound method but different implementation
d=Dog()
d.sound()
c=Cat()
c.sound()
class MathUtils:
def __init__(self, calculation_name):
self.calculation_name = calculation_name # Instance attribute
@staticmethod
def add(x, y):
# No access to 'self' or 'cls'
return x + y
# 1. Call directly using the class name (No instantiation required)
result_class = MathUtils.add(5, 10)
print(result_class) # Output: 15
# 2. Can also be called via an instance
utils = MathUtils("Addition")
result_instance = utils.add(3, 7)
print(result_instance) # Output: 10
# This is called name mangling.
# Python changes it internally.don't directly access this variable from outside.
class Bank:
def __init__(self,balance):
self.__balance = balance
b=Bank(1000)
print(b._Bank__balance)
#better way
class Bank:
def __init__(self,balance):
self.__balance = balance
def show_balance(self):
return self.__balance
b = Bank(1000)
print(b.show_balance())
#insted of self we can use cls
class Student:
school = "ABC School"
@classmethod
def show_school(cls):
print(cls.school)
Student.show_school()