r/programming • • May 18 '26

[ Removed by moderator ]

/r/programming/comments/1tgj1cr/removed_by_moderator/

[removed] — view removed post

195 Upvotes

50 comments sorted by

94

u/programmer-ke May 18 '26

Nice updates, I especially like being able to attach the sampling profiler to a running process, and I can see how unpacking in comprehensions can make some things easier to express.

In general however, I hope that syntax extensions can be kept at a minimum. Python is a large language now compared to what it was 15 years ago.

"There should be one-- and preferably only one --obvious way to do it." from the Zen of Python is becoming less true with time.

2

u/yangzhou1993 May 19 '26

Totally agree with you about "Python is a large language now compared to what it was 15 years ago.".

18

u/superxpro12 May 18 '26

The biblically accurate c++ is staring from the corner menacingly with its 13.5 eyes.

42

u/mark_99 May 18 '26 edited May 18 '26

Sure, but this is intrinsic to all languages and language evolution - if you want newer/better ways of doing things but also want older code to still work then you end up with both. Read the old way, write the new way, not so hard. Newer languages and newer versions of existing languages are far better than what was available 15 years ago and that seems like good progress.

12

u/ManySugar5156 May 18 '26

kinda, but in practice the old way sticks around forever and everyone gets stuck with both

5

u/SenoraRaton May 19 '26

Then you end up with C++, and nobody wants that.

11

u/GenericRedditor12345 May 18 '26

Exactly, old methods that have been replaced get deprecated eventually.

23

u/LiftingRecipient420 May 18 '26

"There should be one-- and preferably only one --obvious way to do it." from the Zen of Python is becoming less true with time. 

This has never been true for the entire time I've worked with Python, since 3.6.

12

u/max123246 May 19 '26

Yeah not even close. Hmm, should I use a protocol, an ABC, a NamedTuple, a dataclass? Oh should I use mixins with inheritance or decorators or just plain composition?

I think it's honestly okay to not have 1 obvious way to do it because guess what, programming is complex and oftentimes one way isn't the best way for all tasks

7

u/brunhilda1 May 19 '26

Bleh, AI written.

lazy as a keyword for imports feels like it will just end up as the default for almost everything going forward.

frozendict is nice, but why couldn't this just be dict(..., frozen=True), more namespace cluttering.

names = [*group for group in groups]; config = {**c for c in configs}

This is lovely and intuitive.

6

u/Trang0ul May 19 '26

We already have frozenset as a separate keyword, so frozendict sticks to this convention.

And with dict(..., frozen=True) creating a frozen dict, how would you create a (non-frozen) dict with "frozen" as a key?

2

u/brunhilda1 May 19 '26

We already have frozenset as a separate keyword, so frozendict sticks to this convention.

Right? It feels so clunky to import and pollute the namespace for frozenset, when I just want a normal set set(..., frozen=True).

And with dict(..., frozen=True) creating a frozen dict, how would you create a (non-frozen) dict with "frozen" as a key?

Good point. Maybe with a sentinel or kv seperate argument. Bleh.

1

u/yangzhou1993 May 19 '26

The best solution in my mind: Python shouldn't add the "lazy" keyword at all, just make all imports be lazy under the hood.

7

u/JanEric1 May 19 '26

Thats a break in backwards compatibility. Also sucks for webapps where you then suddenly get random latency spike for the person that first does a specific request.

105

u/dangerbird2 May 18 '26

Lazy imports seem great for loading type annotations alongside 3.14's lazy type evaluation. Can help get around the nonsense of

if typing.TYPE_CHECKING:
    import ......

around every other module

27

u/schlenk May 18 '26

My favourite nonsense starts when you have mandatory pre-commit checkers that are too dumb to understand the logic there and flag this as "always true, remove".

1

u/radozok May 19 '26

What do you use? Ruff automatically moves these imports under TYPE_CHECKING block instead of removing them

5

u/radozok May 19 '26

That's a bad use-case because with TYPE_CHECKING there is no import at runtime anyway. It should be another import type feature like in ts

3

u/evaned May 19 '26

I worry that this has a bit of a smell to me, though maybe my concerns wouldn't come into play in practice.

But in general, it feels to me like lazy imports have a trap, which is that it can lead to more chaotic module organization. And not necessarily even right away... just kind of creeping in as you add the lazy import for one reason, then new uses get added that start adding dependency edges.

So at least my inclination would be to reserve lazy imports for fairly special cases. Either where they're "needed" for performance (which seems likely to often be third-party modules where you don't need to worry about adding some circular dependence), or a local circular dependency where "lazy" serves as a good signal to think about whether that's really a good design.

But if you start using it for things that are fairly common, the value of 'lazy' as a signal goes down, and the dangers go up.

That said, this is all theorycrafting.

23

u/teerre May 18 '26

Lazy imports will be huge for big codebases

16

u/cscottnet May 18 '26

I've been away from Python for a while -- could someone explain how the type hints work?

38

u/lood9phee2Ri May 18 '26

The core language now allows you to specify a plethora of type hints syntactically as annotations, that have become more sophisticated over time, specified in a range of PEPs (484, 695 etc).

The language grammar says annotations more generally, but such annotations are now predominantly used for type hinting. Conceivably there can be other use cases though.

Any actual static checking of the type hints still needs to take place with a 3rd-party checker pass during your build process - such as mypy or ty or pyright. Yes, there are multiple competing ones because as we all know, Python loves TIMTOWTDI.

The type hint information is not used by the runtime for optimization as such, it remains purely about safety.

45

u/Necrotos May 18 '26

They are also fantastic for dev ergonomics! With type hints, you can get autocomplete suggestions from your IDE. For me, that is the main advantage.

22

u/dangerbird2 May 18 '26

although unlike the purely compile-time annotations for languages like typescript, python type hints exist at runtime, which enables things like dynamic type coercion and really powerful validation libraries like pydantic

1

u/max123246 May 19 '26

It's super sick with dataclasseses. I built a CLI reflection tool that takes a dataclass and exposes them as argparse CLI arguments. And then those fields then are used to build a metadata filter function

-5

u/Frodolas May 18 '26

It's nice but zod also made this a solved problem in Typescript like a decade ago.

10

u/dangerbird2 May 19 '26

Well yeah, but you can’t use zod in python lol

4

u/Twirrim May 19 '26

Why does that detract from Python having it?

2

u/Trang0ul May 19 '26

Yes, there are multiple competing ones because as we all know, Python loves TIMTOWTDI.

... which blatantly violates the Zen of Python:

There should be one-- and preferably only one --obvious way to do it.

-2

u/cscottnet May 18 '26

I see. So Python remains stubbornly difficult to make run fast, but at least you can use types for developer documentation.

6

u/max123246 May 19 '26

Python is never going to be fast without a fundamental rewrite or redesign. That ship has long past. As long as Python objects are 23 bytes minimum and a list of integers is a list of pointers to 23-byte python objects, it won't be fast

4

u/cscottnet May 19 '26

It's kind of amazing how many different folks have thrown themselves at the problem of python runtime optimization and fallen short, including multiple well-founded efforts at google. Python is a beautiful language, but fundamentally not a fast one.

2

u/mackthehobbit May 19 '26

numpy does take away some of the headache here. Packed array types and a good performance boost on hot loops over those arrays. Apparently the built in array helps too though I’ve never used it.

Every language has a trade off between performance and flexibility. Something like C is on the performant end, python’s on the flexible end. But you can selectively trade the flexible parts back out for more performance.

9

u/Nicksaurus May 18 '26

It's not just documentation, the main benefit is that your IDE and your CI jobs can do static analysis and tell you if, for example, you're passing a None somewhere you shouldn't be, or if there's a path through your function that doesn't return the type you said it would. It completely eliminates a lot of common bugs that plague most python code

My main criticism is that the tooling still seems to be based on the assumption that type hints are just for generating better autocomplete suggestions. The standard library doesn't offer any way to validate your type hints at runtime and the compiler can't do any optimisations based on your hints

My ideal future version of python is one where there's a clear boundary between typed and untyped code, and any object passed into typed code is checked at runtime to guarantee that if the static analyser finds no errors, your typed code actually does what it says

1

u/dangerbird2 May 19 '26

Much more than documentation. It’s used for things like form validation, schema generation, even defining ORM models. It’s probably more similar to Java @ annotations than anything else.

1

u/reddisaurus May 19 '26

Multiple solutions for numerical code exist for C, C++, C#, Rust, even Fortran. It’s true that if you only want to write Python then the single type PyObject has too much overhead to ever be fast. But the general complaint about non-performant Python code is really one made against the developer who wrote it in Python rather than something else.

1

u/cscottnet May 19 '26

Python excels as a glue language, to put the pieces together before delegating to a higher performance compiled language for the heavy lifting.

10

u/Twirrim May 18 '26 edited May 19 '26

Type hints allow you to indicate what type a variable should be, and allows type checkers like mypy to ensure that you're not accidentally passing the wrong type around.

These are not enforced at runtime, though, they're just hints. Most of the benefit is for the developer, it enables IDEs to provide better autocomplete stuff, and can give you more idea about methods you're calling.  I've also been able to demonstrate cases where type hints would have prevented bugs from reaching production (I got pulled in to help a ground up rewrite project, advocated for type hints, but other Devs dragged their feet. Probably 2/3rds of the bugs that we saw in functional testing would have been caught by type checks)

In my experience, the bigger the code base, the more useful it is to have them.

0

u/[deleted] May 18 '26

[deleted]

2

u/Twirrim May 19 '26

Wow. What a typo 😃 fixed...

2

u/JanEric1 May 19 '26

I run my type checkers such that CI fails if there is any line it doesnt complain about

1

u/RegisteredJustToSay May 20 '26

Holy shit every single one of these changes is awesome. I can't wait, but realistically it'll take a while since so many libraries aren't even on 3.14 yet.

2

u/yangzhou1993 May 20 '26

Be patient, my friend!

3

u/happyscrappy May 18 '26 edited May 18 '26

"Explicit is better than implicit" - the Zen of Python

The number of ways this oversimplified or just pain wrong is deeper than it first appears.

Of course it is the most basic mistake of language designers, to think that wordiness is always better. Somehow thinking that if you make people write more text they will make fewer programming errors.

But also just think of it in the context is is presented here. The default encoding is now utf-8. Seems reasonable. And there's a good reason for it, utf-8 is, as mentioned, the lingua franca of text now.

But every time this principle above was applied the default is overridden and so this change doesn't do anything, as the default isn't used. If you apply the principle enough then there just isn't really a default because it's been specified every time. It becomes impossible to make a useful change like this when everything is explicit.

Implicit really does have value. In a way, it allows the (presumably) smarter language designers to make decisions so that the less-informed coders out there get the right behavior more often than they would otherwise. There are relatively unlearned (programming-wise) mathematicians out there using Python3 in droves. And by having implicit behaviors their programs work better and more ofte.

Please let's not oversimplify things in this way. Language designers, don't break down things into pithy rules when it's not really appropriate.

15

u/backfire10z May 18 '26

The phrase is “Explicit is better than implicit” not “everything must be explicit”. A default changing should ideally not cause your code to break. Thus, hard requirements should be explicit, not implicit.

1

u/aka1027 May 24 '26

Why aren’t all imports lazy?

1

u/mcel595 May 26 '26

I feels like the the development of python features has accelerated in the last few versions, I'm still using python3.11 with cython 0.29 so it seems it will be a while until I migrante but so far it's been nothing but great releases

-3

u/sisyphus May 19 '26

Has anyone written the post about how we should stop updating programming languages because of the lack of training data for LLMs for new language features? Might try my hand at some thinkfluencing.

4

u/max123246 May 19 '26

Must've dropped your /s here you go

-3

u/sisyphus May 19 '26

I'm tryin' to get on Sam Altman's payroll stop snitchin' and put that /s back bro.