我正在尝试在 python3.9 中创建一个类,以通过从静态方法加载文件来动态加载类的常量值,如下所示:
class SQLQueries(object):
CLIPAPP = SQLQueries.load_query_statement('clinapp.sql')
MENSALIDADES = SQLQueries.load_query_statement('mensalidades.sql')
SINISTRO = SQLQueries.load_query_statement('sinistro.sql')
@staticmethod
def load_query_statement(sql_file, format_params=None):
curr_dir = os.getcwd()
sql_dir = os.path.join(curr_dir, 'queries', sql_file)
with open(sql_dir, 'r') as sql:
query = sql.read()
query = query.format(format_params)
return query
但是这样做时,会产生以下错误:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-49-ddf4f8beb954> in <module>
----> 1 class SQLQueries(object):
2
3 CLIPAPP = SQLQueries.load_query_statement('clinapp.sql')
4
5 MENSALIDADES = SQLQueries.load_query_statement('mensalidades.sql')
<ipython-input-49-ddf4f8beb954> in SQLQueries()
1 class SQLQueries(object):
2
----> 3 CLIPAPP = SQLQueries.load_query_statement('clinapp.sql')
4
5 MENSALIDADES = SQLQueries.load_query_statement('mensalidades.sql')
NameError: name 'SQLQueries' is not defined
我该如何解决?