7 ms·
Python's pre-declared constants are kinda weird
- xg15 2mo agoIsn't "..." then also behaving like True, False and None, i.e. being a lexical token that rewolves to a hardwired value during parsing?
- zahlman 2mo agoIt is, but Ellipsis is just an ordinary pre-defined constant (with the same value).
- xg15 2mo agoYeah, that makes sense.
- chrisweekly 2mo agorewolves? EDIT: ah, "resolves" typo. was v curious about python's mysterious "wolfing" aspects
- xg15 2mo agoYes, sorry, I had typed that on my phone. But yeah, now I want to know more too about python's new type wolfing paradigm.
- b3orn 2mo agoYou wouldn't expect ... = 42 to work syntactically.
- zahlman 2mo agoPast: https://news.ycombinator.com/item?id=49284392 https://news.ycombinator.com/item?id=49284392 (with my comment), https://news.ycombinator.com/item?id=49250370 https://news.ycombinator.com/item?id=49250370 . Nice to see it get attention this time.
- zahlman 2mo ago… I take it back. It would be nice if, just once, we could have a thread about interesting esoteric Python behaviours without people taking it as an invitation to dump their laundry list of things they personally don't (or do, for that matter) like about Python, or to make inane comparisons to other languages (especially JavaScript, for very unclear reasons) while being simply uninformed.
- echelon 2mo agoI 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.
- datakan 2mo agoIt is 30+ years old with all the baggage you would expect. It’s very much a product of its time.
- 9dev 2mo agoIt’s not like that cannot be changed. Look at PHP, which managed to evolve brilliantly over the last decade and gets tons of things right now.
- snitty 2mo agoI keep on hearing people be excited about PHP. Having first attempted to use PHP in early 00s, I simply cannot bring myself to attempt it again. I once had to rewrite large chunks of a site because it simply couldn't deal with the fact that a string had an apostrophe in it.
- 9dev 2mo agoI can only recommend to check out a popular project, maybe Laravel, and read it’s documentation. Alternatively, look at the PHP website itself. The new syntax improvements alone are pretty convincing IMHO, but the engine also got a JIT compiler, a runtime type system that’s fully opt-in, fibers for concurrency, and much more. It’s really worth a second look.
- thayne 2mo agoPHP has evolved a lot, but it also still has a lot of cruft from its earlier days. And it has made breaking changes on a scale python probably couldn't get away with.
- nneonneo 2mo agoThe __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.
- plant-ian 2mo agoI honestly have never even heard of this constant and I feel like I've been using python for a pretty long time. Although maybe my memory for some things just gets garbage collected if I don't use it enough. Does it actually get used that often in real world code? Seems like it might be kind of risky.
- Vexs 2mo agoI see asserts used in production code as part of flow control way too frequently, so I assume the majority of python users aren't aware of the -O flag, much less this behavior- which I too haven't ever heard of. Of recently, I've noticed claude is a big fan of asserts too.
- UqWBcuFx6NV4r 2mo agoYep. I’ve had to tell Claude to basically not use assert. Thankfully it’s very complaint in this one area.
- rcxdude 2mo agoI feel like it's the kind of thing you might wind up caring about if you're micro-optimizing your python, but in my experience that's a losing game and you're better served rewriting it in another language than bothering with trying to speed up the execution of the raw python code (it's not that you can't optimize python code, but only in broader strokes. If you are looking at the bytecode you're in too deep and every time I've seen it tried the code has been ported shortly afterwards).
- 2mo ago
- jMyles 2mo agoI 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/test_the_whole_story.py https://github.com/nucypher/constantSorrow/blob/master/tests...
- neillyons 2mo agoI 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.
- LPisGood 2mo agoIt is certainly the case that isinstance(True, int) returns True, even today.
- jp_sc 2mo agoI got a bug for not remembering it, a couple of years ago: https://jpscaletti.com/p/8/true-false-one-and-zero https://jpscaletti.com/p/8/true-false-one-and-zero
- chlorelladevil 2mo agoHmm. Having read your post, surely the bug is having a function where set(foo, false) removes foo entirely. What if you want foo to have the value False? Even besides the unintended behaviour where 0 is coerced to a boolean value, this function seems poorly designed.
- jp_sc 2mo agoI don't remember the specifics, it might have been a simplification for the example
- Animats 2mo agoMisery is trying to retrofit "bool", True/False, and nil/null to a language. C had to do that. Python had to do that. Getting those wrong is one of the classic language design mistakes. It seems like treating "True" as a value that equates to 1 will work, but then the special cases get you. Like being able to perform arithmetic on True. Common language design boners: - Not building in strings. That's now in the past. Everybody has strings. (Well, C...) - Not building in multidimensional arrays of the numeric types. Everything that number-crunches needs them, and having multiple definitions is Not Fun and may lead to expensive re-copying between different libraries. This is an enormous blind spot in language design. It's one of the reasons FORTRAN, which has good multidimensional numeric arrays, is still often used for number-crunching. - Not standardizing the small vectors (vec2, vec3, vec4) and their matrix friends. Graphics code depends on these, and it's really annoying if there are multiple slightly incompatible implementations. Especially since GPUs have hardware for those types, and you want CPU and GPU to use the same representations. - Not having arrays of bits. Pascal had PACKED ARRAY[0..N] of BOOLEAN but that was lost in later languages. It's useful to have that as a language construct, because most modern CPUs have good hardware for dealing with bit strings, and you'd like the compiler to use it. Most useful languages acquire these features, but, when they come in late, there are multiple similar implementations, and libraries made incompatible by depending on different implementations. (Amusingly, when Second Life switched from Linden Scripting Language to Luau, they initially had True, TRUE, and true all in use, as different types with different semantics. I was able to persuade the devs to unify the boolean types.)
- Lucasoato 2mo agoWow, I wish to understand the internal details of Python implementation that makes it behave in such a way :)
- wildzzz 2mo agoI don't. I have no interest in trying to assign a value to something that's built-in and not meant to be written to. Like who fucking cares that the boolean constants are actually weird little structure that sometimes let you mess with them and other times your edits are ignored? Maybe this is helpful for writing an entry for an obfuscated code challenge but I'm not doing weird shit like that with the code I expect to work between various Python versions and implementations, especially when I'm getting paid to do it.
- UqWBcuFx6NV4r 2mo agoPlease. Such a charged response wasn’t justified at all. Different people are curious about different things. You ask “who fucking cares?”. The answer? You don’t, and the person you’re replying to does.
- wildzzz 2mo agoYou're right, I was feeling a little salty
- moomoo11 2mo agojust 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
- Vedor 2mo agoI'm not mad, but curious - I use Python for years and only dabbled with JS. Could you elaborate what makes Python magnitide worse than JavaScript?
- Chu4eeno 2mo agoDon't mind the web designers calling themselves engineers. There's a lot of annoying issues with Python, but compared to the billions of dollars and thousands of man hours that has been spent trying to fix Javascript and how horrible it still is, it's a perfectly cromulent language.
- wredcoll 2mo agoThere are two kinds of languages, ones that people complain about and ones nobody uses.
- markdown 2mo agoWebmasters would like a word
- moomoo11 2mo agocool insult, yet i chose those 2 languages exactly because they're popular and horrible as examples. let me guess you work somewhere that hired you to bikeshed all day
- monkpit 2mo agoNot OP, but dependency management, for one.
- itissid 2mo agoReally? https://www.destroyallsoftware.com/talks/wat https://www.destroyallsoftware.com/talks/wat
- snitty 2mo agoPython is three scripting languages in a trench-coat.
- hmokiguess 2mo agoPython 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
- nextaccountic 2mo agoSeems like an excellent user for LLMs
- mjr00 2mo ago> Python is awful. > I often work with data scientists and have to productionize their jupyter notebooks I'm not a huge Python fan, despite working with it fulltime, but this feels like mixing correlation and causation. Data scientists would not be writing good, optimized code in any language.
- slashdave 2mo agoWell, yeah. You might need to bring an R notebook into production.
- ks2048 2mo ago> it’s slow For little utilities, it’s faster than a lot of alternatives - just start the interpreter, no compilation needed. It’s all relative, but if you view it as replacing bash scripts for renaming files or running other tools, it’s 100x better.
- applfanboysbgon 2mo agoPython is amazing compared to writing bat/sh scripts. Different languages are for different purposes, using eg. Rust to write system scripts would just be mental. Whether people abuse those languages for purposes they were not intended for is another story, but that doesn't mean the language is inherently bad. And I mean, > and it’s way too easy to do the wrong thing is there another programming language where you believe a data scientist is going to have an easier time writing correct code than Python? Do you think C or Rust or JavaScript or C# make it harder to do the wrong thing?
- denislexic 2mo ago[dead]
- Revanche1367 2mo agoA 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.
- frollogaston 2mo agoJS is a lot less weird than Python. And Python is what I started with and continue to use half the time. The article missed the most common one, __name__ == "__main__" like wtf
- Revanche1367 2mo agoI’m not a regular JS user and haven’t touched the language in a long time, but I remember a (popular?) website for teaching modern JavaScript mentioned that some aspect of the function/macro that returns the type of an object was just plain wrong in a specific and important case. This was years ago however, so maybe the problem isn’t there anymore. Anyhow, I added JS to the list mainly because it is known as a badly designed language and Brendan Eich seems to agree.
- frollogaston 2mo agoThere are certainly weird things about it, but they're all things you can ignore especially in modern times, whereas in Python you're constantly dealing with it head-on.
- Revanche1367 2mo agoTotally agreed there, Python is my main language nowadays because of work but the inconsistencies and lack of some very easy to add syntactic sugar to cover up some of the ugliness (like your example above) in a backward compatible way is constantly irksome and keeps me from really loving my most used tool.
- jherskovic 2mo agoPython 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.
- JodieBenitez 2mo ago> Also, an 'I need a script for $SYSADMIN_TASK but I want to be able to read it later uv + PEP723 make this even better.
- numpad0 2mo ago> 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.
- rmunn 2mo agoIt also helps prevent people from ever redefining them. I mean, if you try to redefine False (in a version of Python that allows it) then you deserve everything that's about to happen to your code... but at the same time, it could possibly lead to a security attack. Redefine False then import some module and get unexpected behavior that you can manipulate to your advantage, somehow. I don't know how that would work, it probably wouldn't... but there's also no reason not to lock those names in and prevent them from ever being redefined.
- persedes 2mo agoLove 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 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-true-false.html?m=1 https://python-history.blogspot.com/2013/11/story-of-none-tr... Explanation from Guido himself
- throwaway314155 2mo agoQueue the hoards of Python haters apparently.
- teddyh 2mo ago*cue *hordes
- YuechenLi 2mo agoPython 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.
- Waterluvian 2mo agoI think Python remains a very good intro language for getting kids in the door because it doesn’t demand too much tedious stuff, is flexible, and lets people actually solve problems rather than doing computer science. In my experience the difficulty is understanding what exactly is important when teaching someone something new. And it largely depends on the goals. What you’d teach some biology undergrad is going to be very different from what you teach a bunch of robotics team high schoolers (and no you’re not teaching them the best language for controls and embedded).
- rmunn 2mo agoWhat is the best language for controls and embedded, in your opinion? I assume from your comment that it's not a great choice as a first language to teach newbies, but I've never done anything in the field of robotics or embedded stuff, so I'm quite ignorant in that area.
- gucci-on-fleek 2mo agoIf 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 https://peps.python.org/pep-0790/#schedule [1]: https://peps.python.org/pep-0781/#backwards-compatibility https://peps.python.org/pep-0781/#backwards-compatibility
- luciana1u 2mo ago[flagged]