[英]How to access external object attributes from the items in a list in Python?
I have a City object that has some attributes, including a list of houses:我有一个具有一些属性的 City object,包括房屋列表:
city1 = {
'name': "Tokio",
'houses': [
{
'code': 1,
'residents': 4
},
{
'code': 2,
'residents': 2
}
]
}
My problem is that, within each object in the list of houses I need to have a method mount_description(self)
that needs to access the name
attribute of the external object City where this list of houses is inside:我的问题是,在房屋列表中的每个 object 中,我需要有一个方法mount_description(self)
需要访问外部 object 城市的name
属性,该房屋列表位于其中:
class House():
code = None
residents = 0
def mount_description(self):
# get 'name' attribute of the external object
class City():
name = None
houses = list()
I know the correct thing would be for the City object to send the name
to the items in the list of houses, but unfortunately due to many details I cannot do this.我知道正确的做法是让城市 object 将name
发送到房屋列表中的项目,但不幸的是,由于许多细节,我无法做到这一点。 I really need each Home object to be able to access the external object where it is located.我真的需要每个 Home object 能够访问它所在的外部 object。
Does anyone know how to do this?有谁知道如何做到这一点? Or if this is possible?或者如果这是可能的?
Give to the house a reference to its host city给房子一个参考它的主办城市
class House():
code = None
residents = 0
city = None
def __init__(self, city, code, residents):
self.city = city
self.code = code
self.residents = residents
#Do what you want here
def mount_description(self):
print(self.city.name)
class City():
name = None
houses = list()
def __init__(self, json):
for house in json['houses']:
self.houses.append(House(self, house['code'], house['residents']))
self.name = json['name']
def set_name(self, newname):
self.name = newname
def print_name(self):
print(self.name)
Then you can do然后你可以做
city1_json = {
'name': "Tokio",
'houses': [
{
'code': 1,
'residents': 4
},
{
'code': 2,
'residents': 2
}
]
}
city = City(city1_json)
city.print_name()
house1 = city.houses[0]
house1.mount_description()
>>> Tokio
>>> Tokio
city.set_name("Paris")
city.print_name()
house1.mount_description()
>>> Paris
>>> Paris
I think you should need to build something like a most simple tree.我认为您应该需要构建类似最简单的树的东西。 For City, you have a list for houses, or children.对于 City,您有一个房屋或儿童清单。 Each house doesn't know where is located, so need a parent for the house, or city.每所房子都不知道位于何处,因此需要房子或城市的父母。
class House():
def __init__(self, code=None, residents=0):
self.code = code
self.residents = residents
self.parent = None
@property
def city(self):
# get 'name' attribute of the external object
if self.parent:
return self.parent.name
else:
return None
class City():
def __init__(self, name):
self.name = name
self.houses = list()
def add_house(self, house):
self.houses.append(house)
house.parent = self
def add_houses(self, houses):
self.houses += houses
for house in houses:
house.parent = self
city_1 = City('Tokyo')
house_1 = House(code=1, residents=4)
house_2 = House(code=2, residents=2)
city_1.add_house(house_1)
city_1.add_house(house_2)
city_2 = City('Chicago')
house_3 = House(code=1, residents=3)
house_4 = House(code=2, residents=5)
city_2.add_houses([house_3, house_4])
>>> house_1.city, house_3.city
('Tokyo', 'Chicago')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.