-1

I want to design a class named houses that holds the street address, asking price, number of bedrooms and number of bathrooms in the house. I would also like methods included to get and set values for each data field.

First time working with classes. I got the class setup and I'm able to print the classes but I'm not sure how or which methods to use.

class houses:

    def __init__(self, Adress, Askingprice, NumOfBedrooms, NumofBathroom):
        self.Adress = Adress
        self.Askingprice = Askingprice
        self.NumOfBedrooms = NumOfBedrooms
        self.NumofBathroom = NumofBathroom

    def HouseDetails(self):
        return "The house is at {} with a price of {} and has {} Bedroom/s and {} bathroom/s" \
        .format(self.Adress, self.Askingprice, self.NumOfBedrooms, self.NumofBathroom)


house1 = houses("Almonaster_Avenue87", "R 500k", 1, 1)
house2 = houses("Audubon_Place33", "R 900k", 3, 3)
house3 = houses("Baronne_Street78", "R800k", 3, 2)
house4 = houses("Basin_Street55", "R700k", 2, 1)
house5 = houses("Bayou_Road11", "R 900", 4, 2)
house6 = houses("Bienville_Street78", "R700k", 2, 2)
house7 = houses("Bourbon_Street45", "R 800k", 4, 1)
house8 = houses("Broad_Street56", "R 900k", 5, 3)

print("\n",house1.HouseDetails())

Please Note

The "R" at asking price is my currency.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Try to break up your question into smaller parts, and solve them one by one. If you want to write get and set methods, [have a look at this SO question](https://stackoverflow.com/questions/18583673/how-do-i-create-a-class-with-get-and-set-methods-in-python). Note, however, that this is _very uncommon_ for Python code, and it might not be what you are after. You can always access object properties directly. – Fons Jun 01 '19 at 20:05
  • 3
    *"I would also like methods included to Get and set values for each data field."* No no no no no no! This isn't Java! We don't do getters and setters! – Aran-Fey Jun 01 '19 at 20:05
  • 2
    I would consider renaming `HouseDetails` to `__str__` ([docs](https://docs.python.org/3/reference/datamodel.html#object.__str__)) so that it's called when you do `str(house_instance)`. – snakecharmerb Jun 01 '19 at 20:10
  • @Aran-Fey I'm not sure if you being sarcastic and if you are actually serious. Like i said first time I'm doing classes. Not even sure if its possible in python. – Anton Buitendag Jun 01 '19 at 20:13
  • @Fons What would be the best language to do this ? – Anton Buitendag Jun 01 '19 at 20:15
  • Why to you want psuedocode and not real code? While it's _possible_ to write getters and setters for class instance attributes, it's generally unnecessary, because of the ability to turn attribute access into function calls at any time and not affect other code using the class by using the built-in [`property`](https://docs.python.org/3/library/functions.html#property) class as a decorator. – martineau Jun 01 '19 at 20:15
  • @snakecharmerb Thanks I'll give it a go – Anton Buitendag Jun 01 '19 at 20:15
  • Anton: When writing real Python code, it's a very good idea to follow the [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/) guidelines, especially with regards to the naming of classes, variables, and function/method names — so I suggest you read it and start following it. – martineau Jun 01 '19 at 20:21
  • @martineau I don't want to but i guess that is the point of the exercise. Because that it is the exact instructions given to me. – Anton Buitendag Jun 01 '19 at 20:23
  • @martineau Thanks I'll definitely have a look at those Guidelines – Anton Buitendag Jun 01 '19 at 20:25
  • Anton: I understand — but note that this requirement could be met by using the `property` class (which shows the preferred way of coding them in Python). The examples in the linked documentation show how to do it. – martineau Jun 01 '19 at 20:30

1 Answers1

1

If you must have getter and setter methods, here's a succinct way to do it using Python's built-in property class. The create_property() utility function shown is a simplified version of recipe 9.21 in the 3rd edition of the Python Cookbook by Beazley & Jones (2013).

def create_property(name):
    """ Utility to define repetitive property methods. """
    storage_name = '_' + name

    @property
    def prop(self):  # getter
        return getattr(self, storage_name)

    @prop.setter
    def prop(self, value):  # setter
        return setattr(self, storage_name, value)

    return prop


class House:
    address = create_property('address')
    asking_price = create_property('asking_price')
    num_bedrooms = create_property('num_bedrooms')
    num_bathrooms = create_property('num_bathrooms')

    def __init__(self, address, asking_price, num_bedrooms, num_bathrooms):
        self.address = address
        self.asking_price = asking_price
        self.num_bedrooms = num_bedrooms
        self.num_bathrooms = num_bathrooms

    def __str__(self):
        return("The house is at {} with a price of {} and has "
               "{} Bedroom/s and {} bathroom/s".format(self.address,
                    self.asking_price, self.num_bedrooms,
                    self.num_bathrooms))


house1 = House("Almonaster Avenue 87", "R 500k", 1, 1)
house2 = House("Audubon Place 33", "R 900k", 3, 3)
house3 = House("Baronne Street 78", "R 800k", 3, 2)
house4 = House("Basin Street 55", "R 700k", 2, 1)
house5 = House("Bayou Road1 1", "R 900", 4, 2)
house6 = House("Bienville Street 78", "R 700k", 2, 2)
house7 = House("Bourbon Street 45", "R 800k", 4, 1)
house8 = House("Broad Street 56", "R 900k", 5, 3)

print(house1)

Output:

The house is at Almonaster Avenue 87 with a price of R 500k and has 1 Bedroom/s and 1 bathroom/s
martineau
  • 119,623
  • 25
  • 170
  • 301