关于使用类型注释时python3中的python递归导入

问题描述 投票:3回答:2

再花了三个小时后,我想我可能得到了解决方案.forward-references

如果没有更好的方法可以做到这一点,我稍后会关闭这个问题并发布解决方案

我正在对程序进行逆向工程(并尝试使用Python实现它)。

我遇到了这个问题

例如,我在ComponentOfCar.py中有一个类,代码如下

__all__ = ("ComponentOfCar");
import Car;
#...#imports other needed packages

class ComponentOfCar(object):
    def __init__(self, owner:Car.Car):
        self.owner = owner;
        self.type = "Default";
        #....#create other attribute

像这样的另一个类(Car.py):

__all__ = ("Car");
import ComponentOfCar;
#...#imports other needed packages

class Car(object):
    __slots__ = ("m_lstComponents",....);
    def __init__(self, data:FlatBuffersBinaryData):
        self.m_lstComponents = list();
        self.LoadFromData(data);
        #....#create other attribute
    def InstallComponen(self, component:ComponentOfCar.ComponentOfCar)->bool:
        self.m_lstComponents.append(component);
        return True;

但在这之后我遇到了这个问题:

Traceback (most recent call last):
  File "Y:\Car.py", line 2, in <module>
    import ComponentOfCar;
  File "Y:\ComponentOfCar.py", line 2, in <module>
    import Car;
  File "Y:\Car.py", line 4, in <module>
    class Car(object):
  File "Y:\Car.py", line 10, in Car
    def InstallComponen(self, component:ComponentOfCar.ComponentOfCar)->bool:
AttributeError: module 'ComponentOfCar' has no attribute 'ComponentOfCar'

原始程序是用编译语言编写的。在内部,类继承非常复杂,它有数百个类,这让我很头疼。

我想通过使用类型注释并将每个类分成单个文件来使这个过程更加清晰。但这必须使用递归导入。

谷歌搜索了半天后,我还没有找到解决办法,所以我来这里寻求帮助。 Python不能像编译语言那样做,或者我只是犯了一些错误?我糊涂了。我该如何解决呢?

抱歉我的英语不好。谢谢你的时间。 :)

详细说明

这是反编译类声明的结构,就像这样(它是c#,整个文件大约有几十万行):

// Namespace: GameNamespace
public class Car
{
    // Fields
    protected List`1<ComponentOfCar> m_lstComponents; 
    public int ObjectID; 

    // Methods
    public void .ctor(Data data);
    public void AddComponent(ComponentOfCar c);
}

// Namespace: GameNamespace
public abstract class ComponentOfCar
{
    // Fields
    protected Car m_owner;

    // Properties
    public Car Owner { get; }

    // Methods
    public Car get_Owner();
    public void .ctor(Car owner);
}

或者我的问题是如何使用python清楚地实现这一点。

是的,在我的想法中,上面显示的方式,我知道这是错的,但我不知道如何使它正确。我不应该把他们分开?或者我可以用另一种方式编写它们(以避免递归导入)并执行与c#相同的操作?

请告诉我解决这个问题的方法,非常感谢你。

再过三个小时后,我想我可能会得到解决方案

forward-references,我正在检查这个。如果没有更好的方法可以做到这一点,我稍后会关闭这个问题并发布该解决方案,它可能会修复我的代码。

python python-3.x forward-reference
2个回答
1
投票

希望这会有所帮助,

component.朋友

class Component:
    def __init__(self, owner, data):
        self.owner = owner
        self.name = data['name']

car.朋友

from component import Component

class Car:
    def __init__(self, data):
        self.components = []
        self.type = data['type']
        self.color = data['color']

    def add_car_components(self, data):
        self.components.append(Component(self, data));


c = Car({'type': 'bus', 'color': 'red'})
c.add_car_components({'name': 'frontdoor'})
c.add_car_components({'name': 'reardoor'})

for component in c.components:
    print(component.owner.type, component.owner.color, component.name)

结果:

->python car.py

bus red frontdoor
bus red reardoor

-1
投票

经过一番测试,我终于得到了解决方案。也许这不是最好的方法(修复递归导入),但它符合我原来的想法。在这个link说。

经过一番思考,我认为这可能被称为前向引用,广告根据该文档链接。我将这两个类改写为如下:

component of car.朋友:

__all__ = ("ComponentOfCar");
import Car;

class ComponentOfCar(object):
    __slots__=("owner","name");
    def __init__(self, owner:'Car.Car',prop:dict={"name":"NoName"}):
        self.owner = owner;
        self.name = prop["name"];

if __name__=="__main__":
  c=Car.Car({"type":"bus","color":"red"});
  door1=ComponentOfCar(c,{"name":"frontdoor"});
  door2=ComponentOfCar(c,{"name":"reardoor"});
  c.InstallComponent(door1);
  c.InstallComponent(door2);

  print("We got the car",c);
  for component in c.m_lstComponents:
    print(component.name,component.owner);

  comp1=c.m_lstComponents[0];#now we look at the first component
  print("the component %s was installed on a %s %s"%(str(comp1),
         comp1.owner.color,comp1.owner.type));

car.朋友:

 __all__ = ("Car");
import ComponentOfCar;

class Car(object):
    __slots__ = ("m_lstComponents","type","color");
    def __init__(self, data:dict):
        self.m_lstComponents = list();
        self.type=data["type"];
        self.color=data["color"];

    def InstallComponent(self, component:'ComponentOfCar.ComponentOfCar')->bool:
        self.m_lstComponents.append(component);
        return True;
if __name__=="__main__":
  c=Car({"type":"bus","color":"red"});
  door1=ComponentOfCar.ComponentOfCar(c,{"name":"frontdoor"});
  door2=ComponentOfCar.ComponentOfCar(c,{"name":"reardoor"});
  c.InstallComponent(door1);
  c.InstallComponent(door2);

  print("We got the car",c);
  for component in c.m_lstComponents:
    print(component.name,component.owner);

  comp1=c.m_lstComponents[0];#now we look at the first component
  print("the component %s was installed on a %s %s"%(str(comp1),comp1.owner.color,comp1.owner.type));

而现在,我可以正确运行此代码,并得到如下输出:

We got the car <__main__.Car object at 0x0000015904C6AAC8>
frontdoor <__main__.Car object at 0x0000015904C6AAC8>
reardoor <__main__.Car object at 0x0000015904C6AAC8>
the component <ComponentOfCar.ComponentOfCar object at 0x0000015904C647F0> was installed on a red bus

现在我可以严格按照汇编代码编写python代码了。我可以继续我的工作。

如果有更好的方法来满足我的需求,请纠正我。

谢谢大家。 :)

© www.soinside.com 2019 - 2024. All rights reserved.