我有一个用于解析数据库并创建n个属性的类,其中每个属性是数据库中数据的NumPy数组,而该属性的名称是数据库中的变量名称。该类有一个参数(srqs
),它是一个字符串列表,其中每个字符串都是要从数据库中提取的变量名。
我该如何使用文档字符串来表示class DataBase(object):
"""Creates an instance of the database and the requested srqs
Attributes:
dbname (str): Database filename to use.
???
"""
def __init__(self, dbname=None, srqs=None):
self.dbname = dbname
self.extract_data()
def extract_data(self):
with OpenDB(self.dbname) as db:
for srq in srqs:
db.__dict__[srq] = db.GetData(srq)
>>>foo = DataBase('/usr/bin/testdb.bar', ['s1', 's2', 'T'])
>>>foo.__dict__
{'dbname': '/usr/bin/testdb.bar', 's1': np.array([0,0,0]), ...}
中的有效变量名将成为该类实例的属性?
Tab Bar
答案 0 :(得分:1)
怎么样(基本上是问题的最后一句话,我想你说得很好):
class DataBase(object):
"""Creates an instance of the database and the requested srqs
Attributes:
dbname (str): Database filename in use.
"""
def __init__(self, dbname=None, srqs=None):
"""
Arguments:
dbname (str): Database filename to use.
srqs (sequence): Valid variable names in srqs will become attributes of an instance of the class
"""
self.dbname = dbname
self.extract_data()