我正在尝试从python 3.7设计具有@dataclass功能的类。我不确定我的情况是否具体。
让我们接受以下虚拟类:
@dataclass()
Person(threading.Thread):
server: str
database: str
name: str
lastName: str
cursor: Type[MySQLDB] = field=(default_factory=connect_to_db() )
def __post_init__(self):
super().__init__(target= self.run(), name= self.name)
conn_string: str = field(init=False, default="Data Source={0};Data Base={1}").__format__(
self.server, self.database)
name_lastname_string: str = field(init=False, default = "SELECT name FROM
[{0}].[names]
where name = [{1}];).__format__(self.database, self.last name())
def run(self):
pass
def connect_to_db():
try:
mysql_connect = mysql.connector.connect(user = self.user,
password = self.password,
host = self.host,
database = self.database)
if (mysql_connect.is_connected()):
return mysql_connect
else:
return None
def last_name(query):
self.cursor.execute(query)
respond = self.cursor.fetchone()
return respond[0]
所以我的问题是,在特定于threading.Thread继承的情况下,这种数据类设计概念将能够赋予我更大的灵活性。还有我要从中定义属性的数据库调用。