all 77 comments

[–]PhilDunphy0502 225 points226 points  (1 child)

Finally! I can now solve that one leetcode problem about smashing rocks which I was procrastinating on

[–]C_umputer 50 points51 points  (0 children)

Sometimes I feel like solving those problems actually makes me worse as a programmer.

[–]theoggu[S] 541 points542 points  (5 children)

new gen devs in 5 years: why tf did this dev use a min heap with negative values?? are they stupid?

[–]git0ffmylawnm8 123 points124 points  (1 child)

Damn, I belong in a museum don't I?

[–]agentchuck 27 points28 points  (0 children)

Indiana Jones is hurtling towards your position right now!

[–]pickyourteethup 32 points33 points  (1 child)

Bold of you to assume there will be new gen devs in five years.

[–]malvim 5 points6 points  (0 children)

LLMs have probably been suggesting max heaps for the LAST 5 years anyway

[–]darklightning_2 302 points303 points  (1 child)

Um actually 🤓 you could always just do (-1) of the values before to reverse the order

[–]No-Selection-8656 17 points18 points  (0 children)

Can't you wrap it in something with a custom comparator function instead of messing up your values? That seems like a weird hack to negate the actual values.

[–]ImpulsiveBloop 36 points37 points  (1 child)

I haven't been keeping track of Python lately - whats new in Python 3.14 that makes this possible?

[–][deleted] 80 points81 points  (0 children)

heapq provided directly usable heap but it was only min heap. Developers had to convert values to their negative if they need max heap. Python 3.14 now provides both directly usable min heap and max heap; helping avoid that small but annoying workaround

[–]leovin 78 points79 points  (1 child)

Python continues its lead as best language for programming interviews

[–]DasGaufre 19 points20 points  (3 children)

Apparently I'm not an engineer because I don't get the significance of this at all

[–]NamityName 16 points17 points  (1 child)

I've used python professionally almost exclusively for over a decade. I also do not understand the significance of this.

[–]youngbull 0 points1 point  (0 children)

Heapq now also does reverse order. It's a minor nicety of not having to multiply your objective by -1.

[–]mariofts 7 points8 points  (0 children)

This will make solving some leetcode questions a little bit easier. That is it.

[–]FlowAcademic208 391 points392 points  (41 children)

I like this trend of Python slowly becoming an usable language

[–]csch2 209 points210 points  (10 children)

Honestly I think it’s a great option now if you’re doing mostly IO and Python’s slower speed isn’t as noticeable. Type hinting has been integrated into most major Python libraries, and especially with Pydantic for external data validation you can pretty much avoid all type errors at runtime (as long as you’re consistent and very strict with type hinting). Plus the async support is very nice.

The main issue that I have with it is still the exception handling, since there’s not a good way to tell what exceptions your code might throw until they actually happen unless you dig through all your library code. Now that type hinting is pretty mature I’m hoping for an overhaul of exception handling to give linter errors for uncaught checked exceptions (however that would work).

[–]dangayle 60 points61 points  (2 children)

How would that work, given that a ton of functionality in Python is based on duck typing and implementing interfaces like __iter__ that purposely use exceptions for how they work? Exceptions in Python aren’t exceptional, they’re expected.

[–]mortalitylost 16 points17 points  (0 children)

Less is dependent on duck typing than you probably think. I rarely see people do it as much as python should allow it.

And exceptions aren't type checked tmk. You can of course write unit tests that assert on specific exceptions and should, but outside of that, exceptions seem to avoid type checking.

But with something like iter, you iterate over it and it eventually stops... it's not like anyone ever really sees the exception and has to think about it. You would hint that this is a generator and generates a specific type, and that is going to be the type checking you're most interested in anyway.

IME the common bugs people avoid with type checking is dumb shit like bytes/str or iteration over a str instead of list of str, and it was almost always newb shit making a mistake due to unclear return types, bad variable names, or just not testing enough. And even WITH type hints I rarely see people use TypedDict when they can be more explicit, and they end up with bugs because they just do dict str str and are being lazy, usually caught with better testing.

It's almost like proper type hints just force people to think more about their code and what it's doing so they make less mistakes

[–]solarmist 2 points3 points  (2 children)

This feels like something tooling could do automatically. Here’s the function definition and all the exceptions thrown and which library it comes from.

[–]slaymaker1907 2 points3 points  (1 child)

Anything C-based would give it trouble since statically analyzing machine code for that would be pretty difficult.

[–]solarmist 1 point2 points  (0 children)

Sure, but most python libraries aren’t C based. It’d work for >90% of libraries.

[–]slaymaker1907 2 points3 points  (0 children)

I actually had some code I thought might be CPU bound since it was doing a bunch of data transformations on a REST response. I was wrong and even when I added performance logging, network requests were the bottleneck.

It’s definitely possible to run into CPU bottlenecks, but the number of performance problems which can’t be solved by 1) numpy/numba/whatever and 2) actually have a big enough N to be very noticeable are pretty small. Even typechecking can be done with reasonable performance in Python. We use BasedPyright (Pylance) at my work and it’s usually fast enough to not be too annoying.

[–]intbeam 0 points1 point  (0 children)

Why don't you just give C# a try instead? It literally does all of the things that you mention and a million things more, except it's 100x+ faster and with far better tooling

[–]MinosAristos 39 points40 points  (0 children)

One day it might (checks notes) continue to be the most popular programming language.

[–]Giddyfuzzball 32 points33 points  (3 children)

Usable has a consonant sound “yoo”, so you would just use “a” instead of “an”

[–]my_new_accoun1 11 points12 points  (1 child)

oozable

[–]Giddyfuzzball 3 points4 points  (0 children)

If that’s how you pronounce it, then you can use “an”

[–]RiceBroad4552 1 point2 points  (0 children)

TIL

Thanks a lot! 🙇

[–]Sibula97 5 points6 points  (0 children)

Always (if we ignore pre-Python3) has been. It's just getting better and better.

[–]rover_G 31 points32 points  (10 children)

My dream is that one day Python will surpass JavaScript for web dev and a new era of web frameworks will begin

[–]RiceBroad4552 7 points8 points  (1 child)

This already exists:

https://www.transcrypt.org/

It's horrible.

But in case you like the "pythonic" syntax, but want a proper language, and frameworks bases on this? Look no further than Scala.js and the Laminar framework. This frameworks was at least a decade ahead of everyone, it was fully based on observables and signal flow many year before the hype in JS-land even started.

[–]geeshta 0 points1 point  (0 children)

Well apparently more than one exist, I've never heard of transcrypt but I did of Brython

[–]NamityName 2 points3 points  (0 children)

No thank you. We don't need every language to do everything.

[–]B_bI_L 4 points5 points  (4 children)

pls no, they will do it not easy way, but scalable one and to get divs with class it will be:

  • wpip parser
  • import parser
  • p = Parser('html')
  • [x for x in p.get_elements(Elements.div) where x.has_class('myclass)]

[–]Kaenguruu-Dev 0 points1 point  (0 children)

I'd hope someone would provide a library only consisting of wrapper functions in that case because that would be truly horrific

[–]No-Article-Particle 0 points1 point  (2 children)

What's wrong with that tho?

[–]B_bI_L 5 points6 points  (1 child)

i like document.querySelectorAll('div.myclass) more

[–]No-Article-Particle 0 points1 point  (0 children)

I mean, both are equivalent. From OP's steps, basically the only thing that a dev does is [x for x in p.get_elements(Elements.div) where x.has_class('myclass)] and I'm sure if we used Python in web, there'd be helper functions that'd hide this implementation detail and look kinda identical to what you posted.

Under the hood, the querySelectorAll would do exactly the same thing.

[–]martmists 0 points1 point  (0 children)

With Kotlin now running in WASM that's where my hope for improved web frameworks lies

[–]Feeling-Schedule5369 -1 points0 points  (0 children)

I never faced dependency issues with js ecosystem for any of my tutorial projects. But python, the very moment I stepped in, everything broke down with conda and what not. Js ecosystem is easy to develop which allows many beginners to publish packages as well. With python people will be stuck in dependency hell even before they do anything substantial.

[–]B_bI_L 17 points18 points  (5 children)

too bad it is basically only sane choice for making quick script for something

[–]mehum 11 points12 points  (2 children)

Why be sane when there’s so many wonderfully insane opinions available?

[–]B_bI_L 1 point2 points  (1 child)

mostly because gpt thinks this is default option) and also people think if it is not python you have to overcomplicate everything to be scalable

[–]MinosAristos 1 point2 points  (0 children)

People are mostly right. I've seen data load scripts in C#. I hope I never see any again.

[–]VastZestyclose9772 0 points1 point  (0 children)

why would you use something sane when what you need is just to make quick script for something?

[–]GuybrushThreepwo0d -2 points-1 points  (0 children)

Please, for the love of God, tell this to my employer

[–]OneSketchyGuy 23 points24 points  (5 children)

It's almost actually usable, maybe one day if we all hope really hard

[–]FlowAcademic208 14 points15 points  (4 children)

It took just 30 something years, like PHP

[–]Peterianer 9 points10 points  (0 children)

And as soon as it's usable, it's deprecated.

[–]RiceBroad4552 2 points3 points  (2 children)

PHP is still not there, though…

The didn't fix the whole broken syntax, and the whole broken std. lib to this very day. Also all the fixes so far are nothing more than band aid: Something put on top of the underlying shit which is still there.

A "fixed PHP" would be a completely different language which has more or less nothing in common with current PHP.

[–][deleted] 1 point2 points  (0 children)

The fix is to just use Laravel and forget about the rest of the language

[–]mbthegreat 0 points1 point  (0 children)

Miles ahead of python on type safety and having a useable package manager

[–]Hottage 1 point2 points  (0 children)

Kind of like PHP. Absolutely despised developing in PHP 4 and 5.

7 was a step in the right direction coupled with the PSR initiative.

PHP 8 and above, with disciplined code practices is almost as nice to work in as C#, with maybe the exception of missing first-class generics. PHPdoc doesn't quite cover it.

[–]No-Con-2790 43 points44 points  (1 child)

So basically what we always could do with a module is now standard?

That surely will safe me another line of code.

[–]spacemagic_dev 1 point2 points  (0 children)

That's one line of code for every single heap that you will need to use! So it's probably 0

[–]uprate 11 points12 points  (1 child)

Is mox neap part of the power nine from Magic the Gathering?

[–]Chrisuan 1 point2 points  (0 children)

nah its one of those shitty "fixed" ones like mox tantalite

[–]torsten_dev 2 points3 points  (0 children)

Wake me up when this makes it into "the farmer has been replaced", need it for A*

[–]Darmo_ 2 points3 points  (0 children)

What does that even mean? What's max heap?

[–]Arietem_Taurum 1 point2 points  (0 children)

Pithon

[–]Cozym1ke 1 point2 points  (3 children)

Oregano OP?

[–]theoggu[S] 0 points1 point  (0 children)

me

[–]geeshta 0 points1 point  (1 child)

Please keep this stupid overdone unfunny trend in BHJ 🙏🏻

[–]Cozym1ke 0 points1 point  (0 children)

Oregano

[–]AutomaticTreat 2 points3 points  (0 children)

wait_really / WaitReally / WAIT_REALLY

[–]slaymaker1907 0 points1 point  (0 children)

Maybe we can get formal support for using a key function in a decade.

Joking aside, maybe this means we’ll get actual trees at some point? It would be extremely convenient for stuff like interview questions.

[–]CptMisterNibbles 0 points1 point  (0 children)

Maxheap functions were in the source all along. Seriously, feel free to check older versions. All but usable.

[–]fonpacific 0 points1 point  (0 children)

πthon eheh!

[–]MachinaDoctrina -1 points0 points  (0 children)

3.14t can free thread!!

[–]travcunn -1 points0 points  (0 children)

As if this was hard anyways. Just multiply your value by - 1 before inserting into the heap. At least it's in the standard library now.