我有:
ClassAttribute:
def __init__(self, string):
### example of string :
### "c1 int not null"
splitted_string = string.split(' ', 2)
self.col_name = splitted_string[0]
self.col_def = splitted_string[1]
if len(splitted_string) == 3:
self.col_option = splitted_string[2]
else:
self.col_option = ""
ClassTable:
def __init__(self, string):
### example of string :
### "create table TABLE_A (c1 int not null, c2 string)"
string_splitted = string.split(' ', 3)
self.table_name = string_splitted[2]
### In real life, I've a list of my attributes
### Example :
### list_attributes = ['c1 int not null', 'c2 string']
self.table_attributes = []
for attr in list_attributes:
self.table_attributes.append(ClassAttribute(attr))
问题:
如何在不迭代list_attributes的情况下调用表中的属性对象?例如,我想添加" not null"第n个属性的选项,我不知道它的位置,但我知道它的名字。
从属性对象中,如何获取表名?
答案 0 :(得分:0)
Evert的建议对我有用:
ClassAttribute:
def __init__(self, table, string):
splitted_string = string.split(' ', 2)
self.table_name = table.table_name
self.col_name = splitted_string[0]
self.col_def = splitted_string[1]
if len(splitted_string) == 3:
self.col_option = splitted_string[2]
else:
self.col_option = ""
ClassTable:
def __init__(self, string):
string_splitted = string.split(' ', 3)
self.table_name = string_splitted[2]
self.table_attributes = []
for attr in list_attributes:
self.table_attributes.append(ClassAttribute(self, attr))