繁体   English   中英

如何从 Python 列表中的项目访问外部 object 属性?

[英]How to access external object attributes from the items in a list in Python?

我有一个具有一些属性的 City object,包括房屋列表:

city1 = {
    'name': "Tokio",
    'houses': [
        {
            'code': 1,
            'residents': 4
        },
        {
            'code': 2,
            'residents': 2
        }
    ]
}

我的问题是,在房屋列表中的每个 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()

我知道正确的做法是让城市 object 将name发送到房屋列表中的项目,但不幸的是,由于许多细节,我无法做到这一点。 我真的需要每个 Home object 能够访问它所在的外部 object。

有谁知道如何做到这一点? 或者如果这是可能的?

给房子一个参考它的主办城市

    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)

然后你可以做

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

我认为您应该需要构建类似最简单的树的东西。 对于 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM