You want to use something like:
class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.__bedrooms = bedrooms
super(Home, self).show_info()
print("For human habitation: ", bedrooms, "bedrooms")
h = Home(2,3000,3)
Storeys: 2 , floor area: 3000
For human habitation: 3 bedrooms
To actually override the method it is pretty simple again using super to call the parent's method:
class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.__bedrooms = bedrooms
def show_info(self):
super(Home, self).show_info()
print("For human habitation: ", self.__bedrooms, "bedrooms")
h = Home(2,3000,3)
h.show_info()
Storeys: 2 , floor area: 3000
For human habitation: 3 bedrooms
In case you are using python2 the syntax is slightly different, you should also inherit from object to support new style classes:
class Edifice(object):
def __init__(self,storeys,area):
self.__storeys = storeys
self.__area = area
def show_info(self):
print('Storeys:{}, floor area: {}'.format(self.__storeys,self.__area))
class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super(Home, self).__init__(storeys, area)
self.__bedrooms = bedrooms
def show_info(self):
super(Home, self).show_info()
print("For human habitation: {} bedrooms.".format(self.__bedrooms)
h = Home(2,3000,3)
h.show_info()
Not sure what you mean by an executable file but you can use if __name__==__main__
in your file and the code will only be executed if you run the file:
if __name__=="__main__":
h = Home(3,3000,2)
h.show_info()
If you just want to import and run the code in another .py
file:
from whatever import Home
h = Home(3,3000,2)
h.show_info()
The whatever module will have to be in the same directory or in your PYTHONPATH
, there are a few different methods of adding to your path discussed in this question.
For your edit, I would make the message an attribute and change it for the second instance:
class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.__bedrooms = bedrooms
self.message = "For human habitation: {} bedrooms"
def show_info(self):
super(Home, self).show_info()
print("{}".format(self.message.format(self.__bedrooms)))
Then:
from module import Home
h = Home(3, 3000, 2)
h.show_info()
h2 = Home(2, 4000, 5)
h2.message = "Second Human habitation: {} bedrooms"
h2.show_info()
Output:
Storeys: 3 , floor area: 3000
For human habitation: 2 bedrooms
Storeys: 2 , floor area: 4000
Second Human habitation: 5 bedrooms
Without mangling it is much simpler:
class Home(Edifice):
def __init__(self, storeys, area, bedrooms):
super().__init__(storeys, area)
self.bedrooms = bedrooms
self.message = "For human habitation: {} bedrooms".format(self.bedrooms)
def show_info(self):
super(Home, self).show_info()
print(self.message)
We just set the whole message from the instance:
from module import Home
h = Home(3, 3000, 2)
h.show_info()
h2 = Home(2, 4000, 5)
h2.message = "Second Human habitation: {} bedrooms".format(h2.bedrooms)
h2.show_info()