I'm trying to create a class in python3.9 to dynamily load constant values of the class by loading a file from a static method as follows:
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
But when doing this, it yields the following error:
---------------------------------------------------------------------------
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
how can I fix that?
By not doing it.
First, you're misunderstanding the execution model of Python in three ways:
In essence,
is syntactic sugar for:
From this you can see two things:
Second, static methods are rarely if ever useful in Python, and as the old Python is not Java essay noted almost 20 years ago:
So
Otherwise... just write a function. Functions are cool. Here you should just write a function, there is no reason for a staticmethod.
I'm not sure the class itself is even useful, frankly.