[英]Understanding Class Inheritance in Python from learningPythonthehardway
[英]python class Inheritance understanding
我想更好地了解如何使用python类继承。
我在网上发现了以下问题。
包括家具类。 在实例化期间,这个类应该要求一个
room
参数。 然后,创建以下从Furnishing
类继承的类:Sofa
,Bookshelf
,Bed
和Table
。使用内置列表类型记录您家中与上述类别匹配的家具。 例如,你可能有:
>>> from furnishings import *
>>> home = []
>>> home.append(Bed('Bedroom'))
>>> home.append(Sofa('Living Room'))
现在,编写一个map_the_home()函数将其转换为内置的dict类型,其中房间是单独的键,相关的值是该房间的家具列表。 如果我们针对我们在命令行中显示的内容运行该代码,我们可能会看到:
>>> 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
?
它会像以下一样简单:
def map_the_home(home):
result = dict([(a.room, a) for a in home])
return result
不是吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.