I have a class that is inheriting from another:
class business2(business):
steps()
The business class has a method from which I want to inherit all that procedure so as not to write the same thing again.
def steps(self):
i = 0
while i < self.quantityY:
years = int(input("year :"))
sales = int(input(str(years)
+" sales value :"))
#we create a list without discount
self.allyears.append(years)
self.witOdiscounts.append(sales)
i+=1
if i == self.quantityY:
for x in self.witOdiscounts:
print(x)
When I run I get this:
steps()
NameError: name 'steps' is not defined
The instance of course:
from exercise6 import *
instance = business2(2)
instance.steps()
What I want is to reuse that method to add more stuff to it and not have to copy and paste it.
If you are going to call the method manually like this:
try the call in the child class with:
surplus.
When you inherit from the parent class, the method is simply inherited, it is exactly as if you had defined it in your child class and it is accessed identically:
If you want to call them automatically when the class is instantiated you must do it from the initializer and use
self.steps
to reference it:If you do this:
the method will be executed at definition time not when you instantiate the class.
In case you want to override the method in your child class but you need the execution of the parent method to be carried out use
super
to call it:As a note, remember that by convention classes are always named using CamelCase to differentiate them from modules, functions, methods and variables that use lowercase and
_
as a word separator.