加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python类继承理解

发布时间:2020-12-20 13:41:35 所属栏目:Python 来源:网络整理
导读:我想更好地了解如何使用 python类继承. 我在网上发现了以下问题. 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 , Books
我想更好地了解如何使用 python类继承.

我在网上发现了以下问题.

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>]}

我正在努力:

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)并让它返回所需的字典?

解决方法

它会像以下一样简单:

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

不是吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读