Given an Enum
object that cannot be modified, and a custom Query
class that should generate a compilation of the Enum
values given different arguments:
from enum import Enum
class Fields(Enum):
a = ["hello", "world"]
b = ["foo", "bar", "sheep"]
c = ["what", "the"]
d = ["vrai", "ment", "cest", "vrai"]
e = ["foofoo"]
class Query:
def __init__(self, a=True, b=True, c=False, d=False, e=False):
self.query_fields = set()
self.query_fields.update(Fields.a.value) if a else None
self.query_fields.update(Fields.b.value) if b else None
self.query_fields.update(Fields.c.value) if c else None
self.query_fields.update(Fields.d.value) if d else None
self.query_fields.update(Fields.e.value) if e else None
It's possible to get a custom set of query_fields
, as such:
[out]:
>>> x = Query()
>>> x.query_fields
{'bar', 'foo', 'hello', 'sheep', 'world'}
>>> x = Query(e=True)
>>> x.query_fields
{'bar', 'foo', 'foofoo', 'hello', 'sheep', 'world'}
Question: In the Query
initialization function, we had to iterate through each class argument and do something like self.query_fields.update(Fields.a.value) if a else None
, are there other ways to achieve the same behavior and output of Query().query_fields
without hard-coding each argument?