• kokada an hour ago

    From this example:

        lazy from typing import Iterator
    
        def stream_events(...) -> Iterator[str]:
            while True:
                yield blocking_get_event(...)
    
        events = stream_events(...)
    
        for event in events:
            consume(event)
    
    Do we finally have "lazy imports" in Python? I think I missed this change. Is this also something from Python 3.15 or earlier?
    • karpetrosyan 7 minutes ago

      Note that you can work around it by implementing `def __getattr__(name: str) -> object:` at the module level on earlier Python versions

      • llimllib 30 minutes ago
        • boxed an hour ago

          Yes, 3.15+

          • rad120 40 minutes ago

            Python is such a weird language. Lazy imports are a bandaid for AI code base monstrosities with 1000 imports (1% of which are probably Shai Hulud now).

            And now even type imports are apparently so slow that you have to disable them if unused during the normal untyped execution.

            If Instagram or others wants a professional language, they should switch to Go or PHP instead of shoehorning strange features into a language that wasn't built for their use cases.

            • stingraycharles 37 minutes ago

              > Python is such a weird language. Lazy imports are a bandaid for AI code base monstrosities with 1000 imports

              Just because you don’t like a feature doesn’t mean it’s because of AI and bad code.

              • sigmoid10 32 minutes ago

                I think this is just a natural consequence of an easy-to-use package system. The exact same story as with node. If you don't want lots of imports, don't make it so damn easy to pile them into projects. I'm frankly surprised we still see so few supply chain attacks, even though they picked up their cadence dramatically.

              • novov 21 minutes ago

                Empirically, I have used the current accepted way to do lazy imports (import statement inside a function) before AI coding was even a mainstream thing, for personal code that sometimes needs a heavy import and sometimes doesn’t.

                The lazy statement would be an improvement as it allows one to see all the imports at the top where you expect them to be.

                • afH12 13 minutes ago

                  As a now deleted comment pointed out, lazy imports had been requested forever. They were rejected forever and were accepted just when BigCorps wanted them.

                  Python-dev now is paid to shore up the failed Instagram stack.

                • formerly_proven 15 minutes ago

                  On most unix-likes all "imports" via shared libraries (e.g. in C / C++) are lazy by default.

                  • undefined 33 minutes ago
                    [deleted]
                    • ziml77 19 minutes ago

                      But also great for speed. Larger libraries can take a measurable amount of time to import (even if they have no transitive dependencies). If only some of your code paths actually need the large library then it makes sense to import it lazily. Without lazy you have to do it conditionally which can lead to the imports happening in strange places rather than all being listed out at the top of the file.

                • JohnKemeny 29 minutes ago

                  > I've left this one to the bonus section because I've never used set operations on Counters and I'm finding it extremely hard to think of a use case for xor specifically. But I do appreciate the devs adding it for completeness.

                  Check out symmetric difference

                  https://en.wikipedia.org/wiki/Symmetric_difference

                  • brianwawok an hour ago

                    I was so into Python for 10 years, was enjoyable to work in. But have deleted 100k+ lines this year already moving them to faster languages in a post AI codebot world. Mostly moving to go these days.

                    • BOOSTERHIDROGEN 15 minutes ago

                      Interested in why you'd use Python in the first place? Advice for someone who knows nothing about programming - what would you suggest?

                      • stuaxo 40 minutes ago

                        This is straightforward in the first instance, but how do you see maintenance of those projects going forward - especially adding more complex features ?

                        I can see one way forward being to prototype them in python and convert.

                        • zabzonk 5 minutes ago

                          Three things I find unlikely about this:

                          - You wrote 100K lines of code

                          - You wrote those lines in Python

                          - You deleted them

                          • physicsguy an hour ago

                            Go is terrible for scientific/ML work though, the libraries just aren't there. The wrapping C API story is weak too even with LLMs to assist.

                            Try and write a signal processing thing with filters, windowing, overlap, etc. - there's no easy way to do it at all with the libraries that exist.

                            • LtWorf an hour ago

                              I think the purpose of go is to write CRUD. Stray from that and you're on your own.

                            • shankysingh an hour ago

                              Thats very intersting, If I may ask was it from professional projects or personal projects?

                              • mountainriver an hour ago

                                Same, I’m not sure how Python survives this outside of machine learning.

                                All of our services we were our are significantly faster and more reliable. We used Rust, it wasn’t hard to do

                                • prodigycorp an hour ago

                                  the funny thing is that everyone, including myself, posited that python would be the winner of the ai coding wars, because of how much training data there is for it. My experience has been the opposite.

                                  • tyre 23 minutes ago

                                    I felt the opposite, because Python isn’t a great language. It won because of Google, fast prototyping, and its ML interop (e.g. pandas, numpy), but as a language it’s always been subpar.

                                    Indentation is a horrible decision (there’s a reason no other language went this way), which led to simple concepts like blocks/lambdas having pretty wild constraints (only one line??)

                                    Type decoration has been a welcome addition, but too slowly iterated on and the native implementations (mypy) are horribly slow at any meaningful size.

                                    Concurrency was never good and its GIL+FFI story has boxed it into a long-term pit of sadness.

                                    I’ve used it for years, but I’m happy to see it go. It didn’t win because it was the best language.

                                    • groundzeros2015 8 minutes ago

                                      I’m always baffled when language complaints come down to syntax

                                    • lsbehe 40 minutes ago

                                      The tons of python code would be great training data if there was any consistency across the ecosystem. Yet every project I've touched required me to learn it's unique style. Then I'd imagine they practically poisoned half the training set because python2 is subtly different.

                                      • lexicality 41 minutes ago

                                        a lot of the training data is either for python 2 or just generally very low quality

                                        • prodigycorp 27 minutes ago

                                          That could be it. I still see LLMs fail a set of static typing challenges that I created a couple years ago as a benchmark. Google models still fail it. I wonder if the lack of typing in a lot of the training data makes python harder to reason about?

                                          • stuaxo 38 minutes ago

                                            The quality issue doesn't seem unique to Python.

                                            The versioning issue I've seen across libraries that version change in many languages.

                                            I don't tend to hit Python 2 issues using LLMs with it, but I do hit library things (e.g. Pydantic likes to make changes between libraries - or loads of the libraries used a lot by AI companies).

                                        • LtWorf an hour ago

                                          You can test on the device directly, without needing to recompile to try something.

                                        • deppep 36 minutes ago

                                          i don’t really see it this way. the value of a token in Python is much higher than it is in lower-level language