python - 如何引用对象自己的容器

标签 python oop

我正在努力更好地使用 Python 中的 OOP,并且在我正在编写的一个程序中遇到了一些真正的黑客行为。它有效,但很困惑。

下面是一个简短的测试示例来说明。它将 0、2 或 4 个 window 的汽车创建到一个列表中,然后将第一个元素与列表的其余部分进行比较。

第一个类的第三种方法表明了我所担心的。我只是希望能够引用特定对象所在的任何容器,而不必每次都从参数中调用它。在这个例子中,它甚至没有那么糟糕,但是我正在做的事情在很多地方都有它,以至于它开始变得令人困惑。

    import random

    class Car:
            def __init__ (self, company, doors, id):
                    self.company = company
                    self.doors = doors
                    self.id = id

            def printDoors(self, id):
                    print 'Car ' + `self.id` + ' has ' + `self.doors` + ' doors.'

            def findSameDoors(self, id):
                    # these next lines are the ones that really bother me
                    companyAbstract = self.company + 's'
                    for i in eval(companyAbstract):
                            if self.id != i.id and self.doors == i.doors:
                                    print 'Car ' + `i.id` + ' does too!'

    class Company:
            def __init__ (self, types):
                    self.types = types

            def typesToNum(self):
                    result = []
                    for i in self.types:
                            if i == 'sedan':
                                    result.append(4)
                            elif i == 'convertible':
                                    result.append(2)
                            else:
                                    result.append(0)
                    return result


    porsche = Company(['sedan', 'convertible'])
    honda = Company(['sedan', 'convertible', 'motorcycle'])

    porsches = []
    for i in range(10):
            porsches.append(Car('porsche', random.choice(porsche.typesToNum()), i))

    hondas = []
    for i in range(10):
            hondas.append(Car('honda', random.choice(honda.typesToNum()), i))


    porsches[0].printDoors(0)
    porsches[0].findSameDoors(0)

以防万一,RHEL 上的 Python 2.4.3。谢谢!

最佳答案

如果我理解你的问题正确,你想将汽车列表附加到公司对象:

import random

class Car:
    def __init__ (self, company, doors, id):
        self.company = company
        self.doors = doors
        self.id = id

    def printDoors(self, id):
        print 'Car ' + `self.id` + ' has ' + `self.doors` + ' doors.'

    def findSameDoors(self, id):
        for i in self.company.cars:
            if self.id != i.id and self.doors == i.doors:
                print 'Car ' + `i.id` + ' does too!'

class Company:
    def __init__ (self, types):
        self.types = types
        self.cars = []

    def typesToNum(self):
        result = []
        for i in self.types:
            if i == 'sedan':
                result.append(4)
            elif i == 'convertible':
                result.append(2)
            else:
                result.append(0)
        return result


porsche = Company(['sedan', 'convertible'])
honda = Company(['sedan', 'convertible', 'motorcycle'])

for i in range(10):
    porsche.cars.append(Car(porsche, random.choice(porsche.typesToNum()), i))

for i in range(10):
    honda.cars.append(Car(honda, random.choice(honda.typesToNum()), i))


porsche.cars[0].printDoors(0)
porsche.cars[0].findSameDoors(0)

可以对其进行更多清理,但我认为这应该可以解决您眼前的问题。

关于python - 如何引用对象自己的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9237725/

相关文章:

python - 使用 OpenCV 3 和 python 3 按区域对图像轮廓进行排序的最佳解决方案

c - 我将如何 'spawn' 或按程序实例化多个生物?

java - 多个参数的多重分派(dispatch)

python - wxPython GUI BoxSizers

python - 如何使用Python获取子类的完整类名

python - 同一包中的 cython 导入错误

perl - 我应该先学习 Perl 5 OO 还是 Moose?

oop - Perl6 : How to load a class module dynamically?

c# - MVP 和多个用户控件

python - 如何在 Python csv writer 中将单引号替换为双引号?