如何使用Python将一个类的ID添加到另一个类?

时间:2018-03-17 08:30:16

标签: python logic

我想将一个类的id添加到另一个类,例如:

(subclass A B) 

1,B (B has the auto increment id )
2,A,1 (because A is the subclass of B , so class A get the id of class B)

输出应该是这样的:

   1,B 
   2,A,1

但是我无法添加班级的ID' A'。那有什么办法吗? 我的代码如下:

with open('aclass.txt', 'r') as in_file:
id = 1
id1 = 0

for line in in_file:
    line = line.replace("(", "")
    text = line.replace(")", "")
    line = (text.split())

    with open('a.csv', 'r+') as out_file:
            line_found = any(line[2] in l for l in out_file)
            if not line_found:
                output = "%d,%s\n" % (id, line[2])
                out_file.write(output)

            line_found = any(line[1] in l for l in out_file)
            if not line_found:
                id1 = id
                id = id + 1
                output = "%d,%s,%d\n" % (id, line[1], id1)
                out_file.write(output)

在这段代码中,首先我从行替换()。然后找到' B'的位置。并给出自动增量ID。然后找到班级的位置' A'。我想知道的是班级' A'从班级&B; B获取其ID?作为' A'是B'类的子类。

0 个答案:

没有答案