I have the following class which produces an error:
class MyClass(object):
QUERIES_AGGS = {
'query3': {
"query": MyClass._make_basic_query,
'aggregations': MyClass._make_query_three_aggregations,
'aggregation_transformations': MyClass._make_query_three_transformations
}
}
@staticmethod
def _make_basic_query():
#some code here
@staticmethod
def _make_query_three_aggregations():
#some code here
@staticmethod
def _make_query_three_transformations(aggs):
#some code here
For now it won't recognize MyClass. If i remove "MyClass" python won't recognize the functions. I know I could move the static methods from inside the class outside as module functions. Is it possible to keep them inside the class and use them like I am trying?
1 Answer 1
Change the order so that the dictionary is specified after the methods have been defined. Also don't use MyClass
when doing so.
class MyClass(object):
@staticmethod
def _make_basic_query():
#some code here
pass
@staticmethod
def _make_query_three_aggregations():
#some code here
pass
@staticmethod
def _make_query_three_transformations(aggs):
#some code here
pass
QUERIES_AGGS = {
'query3': {
"query": _make_basic_query,
'aggregations': _make_query_three_aggregations,
'aggregation_transformations': _make_query_three_transformations
}
}
This works because when in the body of the class declaration, you can reference methods without needing the class type. What you are referencing has to have already been declared though.
MyClass._make_basic_query
, neither the class definition is finished nor the method is defined.