簡體   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