Python 设计缺陷--试图阻止交叉导入

Python 设计缺陷--试图阻止交叉导入,python,import,Python,Import,我面临的实际问题比这个复杂得多,但这基本上可以归结为: World.py import Cell worldobjects = [] for i in range(10): #create a bunch of initial cells worldobjects.append(Cell.Cell()) while True: for obj in worldobjects: obj.update() import Cell worldobjects

我面临的实际问题比这个复杂得多,但这基本上可以归结为:

World.py

import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell())

while True:
    for obj in worldobjects:
        obj.update()
import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell(worldobjects))

while True:
    for obj in worldobjects:
        obj.update()
import Cell
import WorldObjects

for i in range(10): #create  a bunch of initial cells
    WorldObjects.objects.append(Cell.Cell())

while True:
    for obj in WorldObjects.objects:
        obj.update()
Cell.py

from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            worldobjects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
#from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self, worldobjects):
        self.lifetime = 0 #Keep track of frames spent 'alive'
        self.worldobjects = worldobjects

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            self.worldobjects.append(Cell(self.worldobjects)) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
import WorldObjects

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):

        self.lifetime += 1 
        if self.lifetime > 30:
            WorldObjects.objects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime
这个错误有点不具体,但我知道这意味着我不应该交叉导入东西:

ImportError: No module named worldobjects

我知道这是一个设计缺陷,但我不太确定如何以不同的方式解决这个问题。正如您从代码中看到的,每个单元格都应该每30帧“复制”,唯一可行的方法是将它们添加到
World.py
文件中的数组中。我曾考虑将
worldobjects
数组移动到它自己的文件中,但我觉得这有点脏。有人能帮我解决这个问题吗?

在您的手机中保留指向worldobjects的链接怎么样:

Cell.py

from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            worldobjects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
#from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self, worldobjects):
        self.lifetime = 0 #Keep track of frames spent 'alive'
        self.worldobjects = worldobjects

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            self.worldobjects.append(Cell(self.worldobjects)) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
import WorldObjects

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):

        self.lifetime += 1 
        if self.lifetime > 30:
            WorldObjects.objects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime
World.py

import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell())

while True:
    for obj in worldobjects:
        obj.update()
import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell(worldobjects))

while True:
    for obj in worldobjects:
        obj.update()
import Cell
import WorldObjects

for i in range(10): #create  a bunch of initial cells
    WorldObjects.objects.append(Cell.Cell())

while True:
    for obj in WorldObjects.objects:
        obj.update()

在单元格中保留指向worldobjects的链接怎么样:

Cell.py

from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            worldobjects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
#from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self, worldobjects):
        self.lifetime = 0 #Keep track of frames spent 'alive'
        self.worldobjects = worldobjects

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            self.worldobjects.append(Cell(self.worldobjects)) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
import WorldObjects

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):

        self.lifetime += 1 
        if self.lifetime > 30:
            WorldObjects.objects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime
World.py

import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell())

while True:
    for obj in worldobjects:
        obj.update()
import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell(worldobjects))

while True:
    for obj in worldobjects:
        obj.update()
import Cell
import WorldObjects

for i in range(10): #create  a bunch of initial cells
    WorldObjects.objects.append(Cell.Cell())

while True:
    for obj in WorldObjects.objects:
        obj.update()

在单元格中保留指向worldobjects的链接怎么样:

Cell.py

from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            worldobjects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
#from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self, worldobjects):
        self.lifetime = 0 #Keep track of frames spent 'alive'
        self.worldobjects = worldobjects

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            self.worldobjects.append(Cell(self.worldobjects)) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
import WorldObjects

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):

        self.lifetime += 1 
        if self.lifetime > 30:
            WorldObjects.objects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime
World.py

import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell())

while True:
    for obj in worldobjects:
        obj.update()
import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell(worldobjects))

while True:
    for obj in worldobjects:
        obj.update()
import Cell
import WorldObjects

for i in range(10): #create  a bunch of initial cells
    WorldObjects.objects.append(Cell.Cell())

while True:
    for obj in WorldObjects.objects:
        obj.update()

在单元格中保留指向worldobjects的链接怎么样:

Cell.py

from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            worldobjects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
#from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self, worldobjects):
        self.lifetime = 0 #Keep track of frames spent 'alive'
        self.worldobjects = worldobjects

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            self.worldobjects.append(Cell(self.worldobjects)) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
import WorldObjects

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):

        self.lifetime += 1 
        if self.lifetime > 30:
            WorldObjects.objects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime
World.py

import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell())

while True:
    for obj in worldobjects:
        obj.update()
import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell(worldobjects))

while True:
    for obj in worldobjects:
        obj.update()
import Cell
import WorldObjects

for i in range(10): #create  a bunch of initial cells
    WorldObjects.objects.append(Cell.Cell())

while True:
    for obj in WorldObjects.objects:
        obj.update()

这是一个很好的例子,说明了为什么全局变量是一个坏主意。与其说细胞需要知道一个称为worldobjects的全局变量的存在,不如在创建时告诉每个细胞这个变量。例如,您可以在构造函数中传递对它的引用。或者创建一个新函数,该函数封装了新单元的创建和有关世界对象的信息


或者,您可以将生命周期检查逻辑完全移出单元,并将其放入世界更新中。这对我来说更有意义,因为我不会让每个单元格负责创建新的单元格。

这是一个很好的例子,说明了为什么全局变量是一个坏主意。与其说细胞需要知道一个称为worldobjects的全局变量的存在,不如在创建时告诉每个细胞这个变量。例如,您可以在构造函数中传递对它的引用。或者创建一个新函数,该函数封装了新单元的创建和有关世界对象的信息


或者,您可以将生命周期检查逻辑完全移出单元,并将其放入世界更新中。这对我来说更有意义,因为我不会让每个单元格负责创建新的单元格。

这是一个很好的例子,说明了为什么全局变量是一个坏主意。与其说细胞需要知道一个称为worldobjects的全局变量的存在,不如在创建时告诉每个细胞这个变量。例如,您可以在构造函数中传递对它的引用。或者创建一个新函数,该函数封装了新单元的创建和有关世界对象的信息


或者,您可以将生命周期检查逻辑完全移出单元,并将其放入世界更新中。这对我来说更有意义,因为我不会让每个单元格负责创建新的单元格。

这是一个很好的例子,说明了为什么全局变量是一个坏主意。与其说细胞需要知道一个称为worldobjects的全局变量的存在,不如在创建时告诉每个细胞这个变量。例如,您可以在构造函数中传递对它的引用。或者创建一个新函数,该函数封装了新单元的创建和有关世界对象的信息


或者,您可以将生命周期检查逻辑完全移出单元,并将其放入世界更新中。这实际上对我来说更有意义,因为我不会让每个单元格负责创建新的单元格。

尽管我同意@RickyA在这个问题上的观点以及关于所提供样本的设计的所有其他评论,但我在Flask和WSGI容器中遇到了类似的问题,它们需要globals(至少是app object),因此,需要对循环进口提高警惕。它不是从太空来的,但它是一个讨厌的东西

也许下面的方法可以帮助有类似问题的人(包括这里的海报)。因此,一种可能是在单独的文件中提取全局变量,该文件用作导入中介。它不能解决循环导入类中的所有问题,但在适用的情况下非常简单

Cell.py

from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            worldobjects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
#from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self, worldobjects):
        self.lifetime = 0 #Keep track of frames spent 'alive'
        self.worldobjects = worldobjects

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            self.worldobjects.append(Cell(self.worldobjects)) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
import WorldObjects

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):

        self.lifetime += 1 
        if self.lifetime > 30:
            WorldObjects.objects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime
World.py

import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell())

while True:
    for obj in worldobjects:
        obj.update()
import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell(worldobjects))

while True:
    for obj in worldobjects:
        obj.update()
import Cell
import WorldObjects

for i in range(10): #create  a bunch of initial cells
    WorldObjects.objects.append(Cell.Cell())

while True:
    for obj in WorldObjects.objects:
        obj.update()
WorldObjects.py

objects = []

尽管我同意@RickyA在这个问题上的意见以及关于所提供样品设计的所有其他意见,但我在Flask和WSGI容器上遇到了类似的问题,它们需要globals(至少是app object),因此需要对循环导入提高警惕。它不是从太空来的,但它是一个讨厌的东西

也许下面的方法可以帮助有类似问题的人(包括这里的海报)。因此,一种可能是在单独的文件中提取全局变量,该文件用作导入中介。它不能解决循环导入类中的所有问题,但在适用的情况下非常简单

Cell.py

from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            worldobjects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
#from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self, worldobjects):
        self.lifetime = 0 #Keep track of frames spent 'alive'
        self.worldobjects = worldobjects

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            self.worldobjects.append(Cell(self.worldobjects)) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
import WorldObjects

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):

        self.lifetime += 1 
        if self.lifetime > 30:
            WorldObjects.objects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime
World.py

import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell())

while True:
    for obj in worldobjects:
        obj.update()
import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell(worldobjects))

while True:
    for obj in worldobjects:
        obj.update()
import Cell
import WorldObjects

for i in range(10): #create  a bunch of initial cells
    WorldObjects.objects.append(Cell.Cell())

while True:
    for obj in WorldObjects.objects:
        obj.update()
WorldObjects.py

objects = []

尽管我同意@RickyA在这个问题上的意见以及关于所提供样品设计的所有其他意见,但我在Flask和WSGI容器上遇到了类似的问题,它们需要globals(至少是app object),因此需要对循环导入提高警惕。它不是从太空来的,但它是一个讨厌的东西

也许下面的方法可以帮助有类似问题的人(包括这里的海报)。因此,一种可能是在单独的文件中提取全局变量,该文件用作导入中介。它不能解决循环导入类中的所有问题,但在适用的情况下非常简单

Cell.py

from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            worldobjects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
#from World import worldobjects #This is the problem, python does not like cross imports

class Cell:
    def __init__(self, worldobjects):
        self.lifetime = 0 #Keep track of frames spent 'alive'
        self.worldobjects = worldobjects

    def update(self):
        self.lifetime += 1 
        if self.lifetime > 30:
            self.worldobjects.append(Cell(self.worldobjects)) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime 
import WorldObjects

class Cell:
    def __init__(self):
        self.lifetime = 0 #Keep track of frames spent 'alive'

    def update(self):

        self.lifetime += 1 
        if self.lifetime > 30:
            WorldObjects.objects.append(Cell()) #Add a new cell to the world
            self.lifetime = 0 #Reset lifetime
World.py

import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell())

while True:
    for obj in worldobjects:
        obj.update()
import Cell

worldobjects = []

for i in range(10): #create  a bunch of initial cells
    worldobjects.append(Cell.Cell(worldobjects))

while True:
    for obj in worldobjects:
        obj.update()
import Cell
import WorldObjects

for i in range(10): #create  a bunch of initial cells
    WorldObjects.objects.append(Cell.Cell())

while True:
    for obj in WorldObjects.objects:
        obj.update()
WorldObjects.py

objects = []

虽然我同意@RickyA关于这件事的看法以及关于所提供样品设计的所有其他意见,但我在Flas上遇到了类似的问题