Recursionerror: Maximum Recursion Depth Exceeded While Calling A Python Object(algorithmic Change Required)
I am learning about Class Inheritance and overriding methods in python. To implement my learning, I wrote this code to let me better understand how Inheritance and Overriding works
Solution 1:
This line return self.random_digits()
and this line return self.unique_id()
are infinite recursive loops.
I assume what you intended to return was last_two_digits
and id
.
When you return a function call, that function has to execute to return the result of its call. Since you are calling the function you are executing it continues calling itself forever.
Corrected code
# Increasing Recursion Limitimport sys
sys.setrecursionlimit(10000)
import random
# Parent ClassclassUnique_id_creator:
def__init__(self, birthdate, birthmonth, birthyear):
self.date = birthdate
self.month = birthmonth
self.year = birthyear
defrandom_digits(self):
last_two_digits = random.randrange(10, 99)
# convert to a string since you are concatenating the resultreturnstr(last_two_digits)
defunique_id(self):
id = int(self.date + self.month + self.year + self.random_digits())
returnid# Child ClassclassUnique_id_distributer(Unique_id_creator):
def__init__(self, name, birthdate, birthmonth, birthyear):
Unique_id_creator.__init__(self, birthdate, birthmonth, birthyear)
self.name = name
defunique_id(self):
id = Unique_id_creator.unique_id(self)
return"Dear "+ str(self.name) + ", Your Unique Id is: "+ str(id)
citizen1 = Unique_id_distributer("hasan", "01", "11", "2000")
print(citizen1.unique_id())
Additionally, you can eliminate the full call of your parent class by using super()
.
Child class example
# Child ClassclassUnique_id_distributer(Unique_id_creator):
def__init__(self, name, birthdate, birthmonth, birthyear):
super().__init__(birthdate, birthmonth, birthyear)
self.name = name
defunique_id(self):
id = super().unique_id()
return"Dear "+ str(self.name) + ", Your Unique Id is: "+ str(id)
Post a Comment for "Recursionerror: Maximum Recursion Depth Exceeded While Calling A Python Object(algorithmic Change Required)"