logoalt Hacker News

Python's pre-declared constants are kinda weird

166 pointsby rbanffyyesterday at 9:39 PM158 commentsview on HN

Comments

Revanche1367today at 12:35 AM

A lot of Python design decisions have felt weird and off to me but they’ve long justified it by saying that it’s those little ugly design choices that make the language so usable and effective in practice compared to more well-designed languages that hardly anybody uses. I’m not enough of an expert to clearly say if that’s really true, but imo, there’s a repeated pattern of slightly weirdly designed languages becoming super popular: Python, Javascript, perhaps C as well. Or, maybe we only notice the weirdness because these languages are used so much and get nitpicked to no end.

show 2 replies
zahlmanyesterday at 10:21 PM

Past: https://news.ycombinator.com/item?id=49284392 (with my comment), https://news.ycombinator.com/item?id=49250370 .

Nice to see it get attention this time.

jherskovictoday at 12:42 AM

Python has some absolutely kick-ass libraries, even without C. It has Django, for those of us who like developing web apps but never could fall in love with Ruby on Rails. And Django is amazing. I've also yet to see a better language for writing quick ETL scripts and pipelines. Also, an 'I need a script for $SYSADMIN_TASK but I want to be able to read it later.' Anything dominated by external latencies (web, databases, etc) will be fast enough for many uses in Python.

Sure, it's not a language to write a web browser or game engine in. And it is slow. But it has some very strong niches outside of ML/Data science. Personally, I love it. To each their own.

show 1 reply
nneonneoyesterday at 10:51 PM

The __debug__ constant is really weird - any block of code guarded with `if __debug__:` will be entirely omitted from the bytecode under PYTHONOPTIMIZE=1. This and `assert` are the only two examples of real “conditional compilation” in Python. This is also the reason why you cannot assign to __debug__: doing so would make it possible to invalidate the compiler’s assumption about `if __debug__:` statements.

show 2 replies
neillyonsyesterday at 10:55 PM

I remember reading that in early versions of Python there was no built in True and False. Each user would implement this themselves as

True = 1

False = 0

then later these got added to the language. In Python 2 you could still reassign and swap them so that 'if False' was actually true!

True, False = False, True

Python 3 you could no longer reassign them.

show 3 replies
hmokiguessyesterday at 11:33 PM

Python is awful. There are so many one offs in libraries, none agree on a style, it’s slow, and it’s way too easy to do the wrong thing. I often work with data scientists and have to productionize their jupyter notebooks which is pure suboptimal hell. I guess it must be a good easy learning curve for research/scratchpad

show 17 replies
gucci-on-fleektoday at 1:36 AM

If you count pre-release versions, there are actually 7 pre-declared constants, since Python 3.15 (planned for release in November [0]) adds a new constant "TYPE_CHECKING" that should behave like "Ellipsis" and "NotImplemented" do right now [1].

[0]: https://peps.python.org/pep-0790/#schedule

[1]: https://peps.python.org/pep-0781/#backwards-compatibility

xg15yesterday at 10:04 PM

Isn't "..." then also behaving like True, False and None, i.e. being a lexical token that rewolves to a hardwired value during parsing?

show 3 replies
YuechenLitoday at 1:18 AM

Python is just such a weird language in general despite its popularity that I honestly cannot recommend anyone who starts programming to choose Python as their first language, contrary to popular sentiments. I mean, I was one of the first person to start using Python when I was in grad school almost a decade ago when everybody else in my field was still using Matlab for their lab code, for the simply reason that Numpy was less awful than Matlab and I needed something that can easily print graphs to PDFs.

The only thing good I can say about Python nowadays is that it's easy to get started for the first five minutes, and then you'll have to deal with all of its weirdness: significant whitespace, truthiness, duck typing, GIL, distribution/packaging, etc, etc.

I was a big fan of Julia as the potential replacement for Python for science for such a long time and I had evangelized it a lot previously, but recently I've been more and more convinced that JIT/multiple dispatch was only good if you already know how to program well to begin with, which for a lot of academics who are not working in computer science, they write quite horrific code. I think it may be better off to skip Python altogether and write your code in a statically typed language to begin with.

show 4 replies
persedestoday at 12:51 AM

Love the investigation and write up.

Took me down some rabbit holes, but interesting to see the chatter about the fix here:

https://github.com/python/cpython/issues/80233

Initially you could reassign True,False but that was verboten with the switch to python 3! The walrus operator was the one simply an oversight.

https://python-history.blogspot.com/2013/11/story-of-none-tr...

Explanation from Guido himself

numpad0today at 12:49 AM

> True, False, and None are keywords. they aren't identifiers, they're just straight up their own lexical tokens.

Could this be so that the interpreter don't inadvertently manipulate them or pass them to a function? param=None and param="" can be very different.

show 1 reply
snittyyesterday at 11:29 PM

Python is three scripting languages in a trench-coat.

Lucasoatoyesterday at 11:10 PM

Wow, I wish to understand the internal details of Python implementation that makes it behave in such a way :)

show 1 reply
jMylesyesterday at 10:54 PM

I made a constant library for python which I liked some years ago. I wonder if any of my ideas made it in:

https://github.com/nucypher/constantSorrow/blob/master/tests...

show 1 reply
throwaway314155today at 1:09 AM

Queue the hoards of Python haters apparently.

echelonyesterday at 10:21 PM

I used to like Python in the 2010s when it felt like a breath of fresh air relative to PHP and Perl.

Now it feels like a weird PHP itself that is slow, brittle, and dangerous to write code at scale in.

The loose typing, potluck standard library, and horrible package manager (insofar as the community does not know how to package code) all feel so dated.

show 7 replies
luciana1utoday at 1:57 AM

you could shadow True for twenty years and Python just shrugged, then one day it's a SyntaxError and every tutorial you ever wrote breaks. peak Python, honestly.

denislexictoday at 12:22 AM

[dead]

moomoo11yesterday at 11:17 PM

just my 2c but py is honestly one of the worst languages and ecosystems i’ve used in my life.

for all the hate js used to get, py is at least a few magnitudes worse.

my opinion ofc. don’t get mad xD

show 3 replies