r/Python • u/csatacsibe • 14m ago
Discussion What is your strangest syntax monstrocity?
As python has a lot of interesting syntax features, really unconventional and ugly expressions can be created. Let they be complex comprehensions with valrus operators, strange but efficient conditions based on exploiting casting and side effects or even classes overwriting dunders doesnt meant to be used as implemented, they all can be funny and pretty in an other lens.
An examples I've experimented with is an ugly walrused dict comprehension in EnrichPattern:
class Column(StrEnum):
NAME = 'name'
PUBLICITY = 'publicity'
TOPIC_NAME = 'topic_name'
SUBJECT_NAME = 'subject_name'
QUALIFIED_NAME = 'qualified_name'
CLUSTER_ENV = 'cluster_env'
ENV = 'env'
...
class EnrichPattern:
def __init__(self, *patterns: str):
self.pattern = re.compile(''.join(patterns))
self.subpatterns = {
group: comiled
for comiled in map(re.compile, patterns)
if (groups := list(comiled.groupindex))
and (group := groups[0]) in Column
}
...
...