-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuper_function_example.py
More file actions
188 lines (121 loc) · 4.35 KB
/
super_function_example.py
File metadata and controls
188 lines (121 loc) · 4.35 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -------------------------------------------------------------
# PYTHON INHERITANCE EXAMPLE WITH super()
# Author: Learning Example
#
# This file demonstrates:
# 1. Constructor inheritance using super()
# 2. Method overriding using super()
# 3. Common mistakes beginners make and how to fix them
# -------------------------------------------------------------
# =============================================================
# EXAMPLE 1: USING super() WITH CONSTRUCTORS (__init__)
# =============================================================
# Parent Class
class konda:
# Constructor to initialize the name
def __init__(self, name):
self.name = name
# Child Class inheriting from konda
class amba(konda):
# Child constructor
def __init__(self, name, age):
# super() calls the parent class constructor
# This initializes the variable "name"
super().__init__(name)
# New attribute specific to child class
self.age = age
# Creating object of child class
emp = amba("kondareddy", 20)
# Accessing inherited and child attributes
print(emp.name)
print(emp.age)
# -------------------------------------------------------------
# EXPECTED OUTPUT
# kondareddy
# 20
# -------------------------------------------------------------
# =============================================================
# EXAMPLE 2: METHOD OVERRIDING WITH super()
# =============================================================
# Parent class
class konda:
# Method to store name
def reddy(self, name):
self.name = name
# Child class inheriting from konda
class amba(konda):
# Method overriding
# This method has the same name as the parent method
def reddy(self, name, age):
# super() calls the parent class method
# This sets the "name"
super().reddy(name)
# Child class adds a new attribute
self.age = age
# Creating object
emp = amba()
# Calling overridden method
emp.reddy("kondareddy", 20)
# Printing values
print(emp.name)
print(emp.age)
# -------------------------------------------------------------
# EXPECTED OUTPUT
# kondareddy
# 20
# -------------------------------------------------------------
# =============================================================
# MISTAKES I MADE WHILE LEARNING
# =============================================================
# ❌ Mistake 1: Passing parameters while creating object
# when class does not have __init__
# WRONG
# emp = amba("kondareddy",20)
# WHY?
# Because Python automatically calls __init__ when an object
# is created. If __init__ is not defined, Python's default
# constructor does not accept arguments.
# ✔ FIX
# emp = amba()
# emp.reddy("kondareddy",20)
# -------------------------------------------------------------
# ❌ Mistake 2: Calling method without object
# WRONG
# emp = reddy("kondareddy",20)
# WHY?
# reddy() is a class method, not a global function.
# ✔ FIX
# emp.reddy("kondareddy",20)
# -------------------------------------------------------------
# ❌ Mistake 3: Using super() when parent has no method
# Example WRONG usage
# super().__init__(name)
# If parent class does not define __init__,
# Python will raise an error.
# ✔ FIX
# Ensure parent method exists before calling super()
# =============================================================
# COMMON BEGINNER MISTAKES WITH super() AND INHERITANCE
# =============================================================
# 1️⃣ Forgetting to call parent constructor
# This results in missing attributes.
# 2️⃣ Passing wrong number of parameters to super()
# 3️⃣ Not understanding that super() follows
# Python's Method Resolution Order (MRO)
# Example to check MRO:
# print(amba.mro())
# =============================================================
# KEY CONCEPTS DEMONSTRATED
# =============================================================
# 1. Inheritance
# Child class inherits properties from parent class.
# 2. Constructor chaining using super()
# 3. Method overriding
# Child method replaces parent method but can still call it.
# 4. Code reuse
# super() allows reuse of parent functionality.
# =============================================================
# SIMPLE RULE TO REMEMBER
# =============================================================
# super() → used to call parent class methods
# especially constructors (__init__)