Hacker News new | ask | show | jobs
by hvocode 1732 days ago
Part of me wants to disagree since some of these features make my code shorter, but I have to agree since they commit a sin of language design that I dislike - implicit magic. Decorators are useful because they can help you shrink code by letting the decorator generate boilerplate for you. The code gets smaller, but now it’s harder to know what’s going on since you need to know what magic happened behind the scenes due to the decorator. It feels very much like issues I ran into when I used C++ meta programming libraries - my code shrank, and at the same time my understanding of what it actually did also shrank. Same with the walruses - my code gets smaller because now there’s some implicit stuff happening.

Comprehensions are a little less magical - if anything, they are more explicit. If I want to create a list where each element is generated by some function over another list, I just say it. Doing so with loops is obscuring what I wanted to say in the first place. The problem with comprehensions isn’t so much the comprehension, but the obtuse ways people can use them to eke out performance by avoiding explicit loops.

I’m all for things that are closer to what a programmer means, but less keen on features that entail obscuring details that may come back to haunt the programmer later (I see this most often with decorators).

2 comments

I don't really agree that it's so magical, especially the walrus operator. Sure, a beginner may be confused by it, but it's really easy once you learn the difference between `=` and `:=` IMO. Python is not a language designed to be only used by beginners. Decorators may be a little more "magic", but it makes little difference in practice if you do @bar or foo = bar(foo), the former is just a cleaner syntax (and stops linters wanting you to put two blank lines between the function and the "decorator").
Oh c'mon with the walrus. Assignment being an expression is the case in so many languages. They should have made it the case in Python and just be done with it.

"But someone will make a typo one day resulting in = instead of ==" argument is nonsense as that hasn't been an issue since forever in other languages. You can either require parentheses if you want to use the value (the way C compilers want it) or just make it := to begin with.