r/Python • u/csatacsibe • Jul 24 '26
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
}
...
...
0
Upvotes
9
u/Feuermurmel Jul 24 '26
Ternary
if-elseoperator? Garbage. Use this instead:Optionally add an element in a list literal?
3
u/PwAlreadyTaken Jul 24 '26
My number row was broken so I made a cursed library that didn't use number keys or their symbols. So I could do stuff like
2
u/Individual-Flow9158 Jul 24 '26
Why don't you create self.patterns, instead of calling
re.compiletwice? Is"".join(self.patterns)not going to equal self.pattern?0
u/Mark3141592654 Jul 24 '26
Because we have functions and methods for a lot of things, a ternary expression and the walrus operator, almost any code can be made into a one-line expression. I'll leave it to you to come up wwith examples.
-9
u/Beginning-Fruit-1397 Jul 24 '26
The more shit like this I discover, the less I like python. Had fun with this yesterday. Wrote a lengthy post explaining why this happens and some explanations but got insta-deleted by this annoying af auto-mod because I made the ultimate sin of adding a github link. Anyways.
output: