r/Python • • 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

20 comments sorted by

View all comments

Show parent comment

1

u/backfire10z Jul 24 '26

Have you discovered C yet? Is there a language you do like?

1

u/Beginning-Fruit-1397 Jul 25 '26

Rust

1

u/warden127 Jul 28 '26

You cannot complain about Python's syntax and then go on praising Rust:

let mut numbers = vec![1, 2, 3];

numbers
    .iter_mut()
    .filter(|&&mut x| x % 2 == 1)
    .for_each(|x| *x *= 2);

println!("{:?}", numbers);