再花费三个小时后,我想我可以得到解决方案。 如果没有更好的方法来执行此操作,我将稍后再关闭此问题,并发布该解决方案 我在程序上进行逆向工程(并尝试使用Python实施(。 我有这个问题 例如我在componentofcar.py中有一个类,代码如下 这样的另一类(car.py(: 但是,在这样做之后,我遇到了这个问题: 原始程序以编译语言编写。在其中,该类的继承非常复杂,并且有数百个课程,这让我头痛。 我想通过使用键入注释并将每个类分开为单个文件,使此过程更清楚。但这必须使用递归导入。 谷歌搜索半天后,我找不到解决方案,所以我来这里寻求帮助。Python不能像编译语言一样这样做,还是我只是犯了一些错误?我很困惑。我该如何修复。 对不起,我的英语破损。感谢您的时间。:( 要详细 这是像这样的分解类声明的结构(是C#,整个文件大约是十万行(: 或我的问题是如何使用Python清楚地实施。 是的,在我的思想中,这样做的方式表明,我知道这是错误的,但是我不知道如何使它正确。我不应该分开它们吗?或者,我可以让它们以另一种方式(避免救援进口(写成,并且可以做与c#? 请告诉我一种解决这个问题的方法,非常感谢。 又花了三个小时后,我认为我可以得到解决方案 正向引用,我正在检查此。而且,如果没有更好的方法来执行此操作,我稍后再关闭此问题并发布该解决方案,它可能会修复我的代码。__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
__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'
// 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);
}
希望这会有所帮助,
component.py
class Component:
def __init__(self, owner, data):
self.owner = owner
self.name = data['name']
car.py
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
测试后,我终于得到了解决方案。也许这不是最好的方法(修复递归导入(,但它符合我的原始想法。在此链接中说。
经过一番思考,我认为这可能被称为前向引用,根据该DOC链接进行广告。我将这两个类改写为波纹管:
componentofcar.py:
__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.py:
__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代码。我可以继续工作。
,如果有更好的方法可以满足我的需求,请纠正我。
感谢大家。:(