python - Inheritance and classes -
this in class file.
class edifice: def __init__(self,storeys,area): self.__storeys = storeys self.__area = area def show_info(self): print('storeys:',self.__storeys,', floor area:',self.__area) 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")
this in executable file:
from classes import edifice, home def main(): print("home") h = home(2,3000,3) h.show_info() main()
from executable file need create instance of home show_info() method different text output.
you want use 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 human habitation: 3 bedrooms
to override method pretty simple again using super call 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 human habitation: 3 bedrooms
in case using python2 syntax different, should inherit object 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 mean executable file can use if __name__==__main__
in file , code executed if run file:
if __name__=="__main__": h = home(3,3000,2) h.show_info()
if want import , run code in .py
file:
from whatever import home h = home(3,3000,2) h.show_info()
the whatever module have in same directory or in pythonpath
, there few different methods of adding path discussed in question.
for edit, make message attribute , change 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 human habitation: 2 bedrooms storeys: 2 , floor area: 4000 second human habitation: 5 bedrooms
without mangling 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 set whole message 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()
Comments
Post a Comment