Python inheritance

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

I found the following problem on the Internet.

Include class furniture. At instantiation, this class must request an argument room. Then create the following classes that inherit from the class Furnishing: Sofa, Bookshelf, Bedand Table.

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

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

Now write the map_the_home () function to convert it to the built-in dict type, where the numbers are separate keys and the associated value is the furniture list for this room. If we run this code against what we show on our command line, we can see:

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

:

class Furnishing(object):

    def __init__(self, room):

        self.room = room

class Sofa(Furnishing):

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

, map_to_home(home) dict?

+4
1

, :

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

?

+1

Source: https://habr.com/ru/post/1534468/


All Articles