2

This is in my 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 is in my Executable file:

from Classes import Edifice, Home

def main():
    print("Home")
    h = Home(2,3000,3)
    h.show_info()

main()

From the executable file I need to create another instance of Home show_info() method with a different text output.

Kate Bliss
  • 97
  • 1
  • 1
  • 6
  • 4
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Martijn Pieters May 31 '15 at 22:01
  • yeah I understand. I am extrememly new to Python, and the tutroaisl I have been watching arent doing a very good job of making me understand this because they dont show anything similar. – Kate Bliss May 31 '15 at 22:04
  • Added what I had though. – Kate Bliss May 31 '15 at 22:19
  • 1
    Much better, because that gives us a far better picture of what you already know and where to best help you. – Martijn Pieters May 31 '15 at 23:12

2 Answers2

1

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()
Community
  • 1
  • 1
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 1
    @Makoto, *overrides the show_info() method by: calling the Edifice class show_info() method to display the storeys and area*, also not exactly rocket science to put it in a method. You would also have to call the method first to match the expected output – Padraic Cunningham May 31 '15 at 22:27
  • 1
    From the description, I'm reading that it still has to override `show_info` in the parent. If it isn't, then you try to invoke that method on an instance of `Home` and you won't get the right information back. – Makoto May 31 '15 at 22:30
  • 1
    @Makoto,I am taking the description wording pretty lightly based on the fact the OP is printing in the init method so the actual desired implementation is not exactly obvious – Padraic Cunningham May 31 '15 at 22:32
  • Also if it makes a difference this is only part of my whole question, but I didn't want to ask the whole thing, just wanted 1 part to see if I could do it. The problem also requires a separate file that executes this. I edited my post with it. – Kate Bliss May 31 '15 at 22:40
  • 1
    @KateBliss, what do you mean by a separate file? Do you mean the `h = Home(2,3000,3)` part? – Padraic Cunningham May 31 '15 at 22:43
  • Thank you. I think I need it just to be imported. I am goign to work on the other classes of the problem now since I can see atleast an outline of how to do it. It just says Create two seperate files one for the classes and one that executes the showinfo() method of each class. – Kate Bliss May 31 '15 at 23:05
  • 1
    @KateBliss, no prob, sounds like importing is exactly what you want. – Padraic Cunningham May 31 '15 at 23:07
  • I have another question if you dont mind. I need to make another instance of Home using its showinfo() method, but it needs to be done in the executable file, and show different print output then the first home instance. It needs to output: "Second Human habitation output" – Kate Bliss Jun 01 '15 at 00:03
  • 1
    @KateBliss. so the last line should be changed to that? – Padraic Cunningham Jun 01 '15 at 00:15
  • Need a whole other instance of it just with different text, yeah. I eidted my question to display easier. – Kate Bliss Jun 01 '15 at 00:21
  • no did not have to. But thank you for the great help :) – Kate Bliss Jun 01 '15 at 00:36
  • 1
    @KateBliss, no prob, I edited to use it anyway but if you have normal attributes you can simply set the message attribute and call instance.bedrooms to get the bedroom count. – Padraic Cunningham Jun 01 '15 at 00:38
1

You're pretty close. The main thing here is to remember to call show_info on the parent class, then add your information in while overriding the show_info declaration.

I presume you're using Python 3 for this, so the below is Python 3 syntax. This also works in Python 2; bear in mind that the parent class will be printing a tuple instead of a string unless you import print_function, as in from __future__ import print_function.

class Home(Edifice):
    def __init__(self, storeys, area, bedrooms):
        Edifice.__init__(self, storeys, area)
        self.__bedrooms = bedrooms

    def show_info(self):
        Edifice.show_info(self)
        print('For human habitation: {} bedrooms'.format(self.__bedrooms))
Makoto
  • 104,088
  • 27
  • 192
  • 230