Live data from Hacker News

What's New in Python 3.12

docs.python.org

61–70 of 120 posts

Re: What's New in Python 3.12

#61
post #43

Earlier quoted context omitted.

Of course this only works at one level and not arbitrarily deeply, but you still need to check for None with this code; it may actually be better to write `x.get("key") or {}` so that you always get an empty dict. I write 'may' because the difference between None and an empty dict may be very subtle and rarely does the API specify with enough precision what are supposed to be the semantics of each case.

> x.get("key") or {} This has the side effect of replacing falsy values like False or 0 with an empty dictionary instead of giving you the actual value. For the exact intended behaviour, you do need to explicitly check for None instead of using a short circuit trick with or.

Yeah, when trying to drill down into deeply nested structures, it'd be a waste to just blindly nullish-coalesce any None objects into empty dicts instead of just immediately short-circuiting out of the deep access. It's a cute trick but would not pass code review in my shop.

Re: What's New in Python 3.12

#62

PEP 695 is great. I've been using mypy every day at work in last couple years or so with very strict parameters (no any type etc) and I have experience writing real life programs with Rust, Agda, and some Haskell before, so I'm familiar with strict type systems. I'm sure many will disagree with me but these are my very honest opinions as a professional who uses Python types every day: * Some types are better than no…

Are there any other languages where typing is purely optional and yet so much effort is going into developing the typing system? I started experimenting with Kotlin just when Python was starting to get type hints and after my experience with Kotlin I am totally sold on the benefits of typing. So to me it's great to see these recent developments. It just feels weird to me to have a language that is fundamentally dynam…

Javascript, with TypeScript, and Ruby come to mind.

Re: What's New in Python 3.12

#63

I suspect people will use these comments to share their wishlist of Python features, so let me add that I really wish Python had a safe navigation operator, for when you are dealing with nested objects that could be None. I have been trying to parse a lot of XML and JSON in Python lately and a feature like that could really help reduce boilerplate checks. https://en.wikipedia.org/wiki/Safe_navigation_operator

For JSON there is the `jmespath` library which might help.

https://github.com/jmespath/jmespath.py

    print(jmespath.search(
        expression="some.deep.nested.value",
        data={"some": {"deep": {"nested": {"value": 2}}}},
    ))
    prints 2

    print(jmespath.search(
        expression="some.deep.nested.value2",
        data={"some": {"deep": {"nested": {"value": 2}}}},
    ))
    prints None

Re: What's New in Python 3.12

#65

I suspect people will use these comments to share their wishlist of Python features, so let me add that I really wish Python had a safe navigation operator, for when you are dealing with nested objects that could be None. I have been trying to parse a lot of XML and JSON in Python lately and a feature like that could really help reduce boilerplate checks. https://en.wikipedia.org/wiki/Safe_navigation_operator

def safe_navigation(obj, path: List[str], default=None): try: for key in path: obj = obj[key] return obj except KeyError: return default Yeah. I thought it would just be a try-except, but that turned out pretty ugly. But really, how do you know if your path is missing or the object at the end of the path is None, without using exceptions for communicating this?

I would write:

    def safe_navigation(obj: Any, *path: str, default: Any = None, strict: bool = False) -> Any:
        sentinel = object() if strict else None
        for key in path:
            obj = obj.get(key, sentinel)
            if obj is sentinel:
                return default
        return obj
The only new disadvantage of this is that it only works on mappings and no longer works on sequences.

But I really don't like having this much `Any`; it's generally a sign of poor design.

Re: What's New in Python 3.12

#66

PEP 695 is great. I've been using mypy every day at work in last couple years or so with very strict parameters (no any type etc) and I have experience writing real life programs with Rust, Agda, and some Haskell before, so I'm familiar with strict type systems. I'm sure many will disagree with me but these are my very honest opinions as a professional who uses Python types every day: * Some types are better than no…

Are there any other languages where typing is purely optional and yet so much effort is going into developing the typing system? I started experimenting with Kotlin just when Python was starting to get type hints and after my experience with Kotlin I am totally sold on the benefits of typing. So to me it's great to see these recent developments. It just feels weird to me to have a language that is fundamentally dynam…

Typing is nice just for the documentation it provides. Refactoring old python scripts was a pain because you had no idea what the arguments and return values were.

Is date a string or an object? It gets tiresome.

Re: What's New in Python 3.12

#67
I find the feature-happy kitchen sink product philosophy in programming languages quite insufferable.

It has become an unpleasant chore to keep up with PEP squabbles and accepted changes.

Re: What's New in Python 3.12

#68
post #4

What I would love to see in a future version of python is being able to do `user["email"]` or `user.email` independently of the reason. Sometimes both work, sometimes only one of the two and an error in throw for the other one. I don't care why, I just want it to work, it's such a basic feature. Something even crazier would be to have an equivalent of `console.log` in python. It would be an amazing feature but I thin…

A dict_to_object function would be a good little intermediate exercise.

Re: What's New in Python 3.12

#69

PEP 695 is great. I've been using mypy every day at work in last couple years or so with very strict parameters (no any type etc) and I have experience writing real life programs with Rust, Agda, and some Haskell before, so I'm familiar with strict type systems. I'm sure many will disagree with me but these are my very honest opinions as a professional who uses Python types every day: * Some types are better than no…

I've been steadily finding the love for F# for sort-of-similar-but-not-quite-the-same reasons. I like Python. It's still my go-to language for most things. But I definitely feel a tail off in utility as code base size increases, and I find myself asking mentally "but what type is that thing?". F# is sort of the opposite of Python. It's strongly, statically and soundly typed, based on Hindley-Milner. It has very good…

Similar sentiment with Rust (love the ecosystem, and love the language, except for the many, many parts of it that I don't need). F# comes up a lot in discussions like this. From a thread a few days ago: https://news.ycombinator.com/item?id=37892666

Re: What's New in Python 3.12

#70
post #67

I find the feature-happy kitchen sink product philosophy in programming languages quite insufferable. It has become an unpleasant chore to keep up with PEP squabbles and accepted changes.

Why do you have to keep up with them?
Post reply on HN