Trying_hard
Trying_hard

Reputation: 9501

python class Inheritance understanding

I am trying to better understand how to work with python class inheritance.

I found the following problem online.

Include a Furnishing class. During instantiation, this class should ask for a room argument. Then, create the following classes that inherit from the Furnishing class: Sofa, Bookshelf, Bed, and Table.

Use the built-in list type to record the furniture in your own home that matches the classes above. So for example, you might have:

>>> from furnishings import * 
>>> home = [] 
>>> home.append(Bed('Bedroom'))
>>> home.append(Sofa('Living Room')) 

Now, write a map_the_home() function to convert that to a built-in dict type where the rooms are the individual keys and the associated value is the list of furniture for that room. If we ran that code against what we display in our command line, we might see:

>>> map_the_home(home){'Bedroom': [<__main__.Bed object at 0x39f3b0>], 'Living Room':[<__main__.Sofa object at 0x39f3b0>]} 

I am trying to work with:

class Furnishing(object):

    def __init__(self, room):

        self.room = room

class Sofa(Furnishing):

    "Not sure how to get this into a dict"???

I am not sure how to call map_to_home(home) and get it to return the desired dict?

Upvotes: 4

Views: 214

Answers (1)

sharcashmo
sharcashmo

Reputation: 815

It would be as easy as:

def map_the_home(home):
    result = dict([(a.room, a) for a in home])
    return result

isn't it?

Upvotes: 1

Related Questions