Live data from Hacker News

What's New in Python 3.12

docs.python.org

111–120 of 120 posts

Re: What's New in Python 3.12

#111

Earlier quoted context omitted.

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?

There's certainly humor in a function called "safe navigation" mutating the data structure I'm supposedly safely navigating through :D

I did type it on my phone, so I’m sure it needs a little correction (particularly the choice of exceptions is not optimal, I think) but feel free to run it in any recent python interpreter and return back with any mutation you see.

Re: What's New in Python 3.12

#112
post #87
post #82

Earlier quoted context omitted.

It is a great language for UNIX scripting, unless one happens to still live in Perl world, and a good enough modern replacement for BASIC.

Unless you want to package and share that tool with your coworkers :P

It works great, because my coworkers use the same Python version as imposed by IT.

Re: What's New in Python 3.12

#113
post #79

Earlier quoted context omitted.

You can't confuse keys in a hash table with properties of objects. It's not confusing, it's a useful distinction. Most languages that have both objects and associative arrays (so not Lua or JavaScript) make this distinction. It's not particularly error prone. You're simply refusing ("don't care why") to learn how to use the tool you're given.

user dictionnary => user["email"] user instance from a django model => user.email It is error prone. You're simply refusing to see an aberration. That's how the language works, but it doesn't mean it's intuitive and easy to understand especially for a language known for being easy to use and understand.

It's not how the language "works", it's what the language offers.

When all you have is a hash table, or when all you have is an object, you get to refer to keys and properties uniformly - because they are the same thing. When you have both, you refer to them differently - because they're different. That's it. There are some languages where objects and hash tables use the same syntax for access even though they're different things, but... you probably never used any of them, and certainly none of them is in the Top 20 on TIOBE.

I'll kick the venerable HN guidelines aside for a second and mention this: you're being heavily downvoted, all your comments in this thread are in various shades of gray, and many people offer many different arguments as to why you're wrong. Yet, you're undaunted and continue posting - I don't want to break the guidelines that much, but honestly, it reads like trolling. You don't engage with the arguments, you're just repeating the same thesis over and over again, without citing evidence. Why?

Re: What's New in Python 3.12

#114

So many breaking changes promulgated with covert version numbers—an unfortunate truth for Python and why the writing on the wall is clear—Python is not dependable—Go is the safer choice for business applications and services.

> So many breaking changes promulgated with covert version numbers

This has been the new strategy to avoid another python2-python3 debacle. As long as you're aware it's not semver and minor versions can have breaking changes it's no biggie.

Re: What's New in Python 3.12

#115

Earlier quoted context omitted.

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?

There's certainly humor in a function called "safe navigation" mutating the data structure I'm supposedly safely navigating through :D

    $ python3
    Python 3.11.6 (main, Oct  2 2023, 20:46:14) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from typing import List
    >>> def safe_navigation(obj, path: List[str], default=None):
    ...     try:
    ...         for key in path:
    ...             obj = obj[key]
    ...         return obj
    ...     except KeyError:
    ...         return default 
    ... 
    >>> obj = {1: {1: {1: 2}}}
    >>> safe_navigation(obj, [1,1,1])
    2
    >>> obj
    {1: {1: {1: 2}}}

Re: What's New in Python 3.12

#116

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

I haven't even used Python for like four years now, but all of these suggestions to do try except and get.key() or {} seem to be ignoring that Python has had a defaultdict in the standard library for, I don't even know, at least the last decade and a half, as long as I've been paying attention. It's even written in C: https://github.com/python/cpython/blob/d9246c7b734b8958da034... .

defaultdict doesn't seem to easily solve for the main topic of deeply nested values.

Re: What's New in Python 3.12

#117
post #25

https://docs.python.org/3/whatsnew/3.12.html#whatsnew312-pep... "PEP 692: Using TypedDict for more precise *kwargs typing" seems pretty great. That'll be really helpful for nested method calls which proxy a lot of their arguments along. I can also see that helping out when creating stub libraries.

… but the Unpack[…] wrapping required is awful, required because they initially defined kwargs type annotations in a rather baffling way.

(I don’t know about others, but it was obvious to me from the start that the way the *args and **kwargs type annotations refer to the sequence values rather than the args/kwargs bindings was always going to cause confusion, and was very likely to cause problems down the line when they wanted more precise typing; and that day has arrived for kwargs, no idea if they have any plans for similar on args, which is sometimes useful. They should just have required `*args: List[T], **kwargs: Dict[str, T]` from the start.)

Re: What's New in Python 3.12

#118
post #105

Earlier quoted context omitted.

I wouldn't call a function that mutates the input "safe navigation", personally :)

Er, it doesn't? `.get()` (or `[]` in the original) doesn't change the object, and `obj =` only changes which object the local variable refers to? This isn't like C++ with its intrusive `=`.

You're right. Python is pass by reference and it's the reference being changed, not the object. This is what happens when you read code on the internet after drinks.

Re: What's New in Python 3.12

#119
post #107

Earlier quoted context omitted.

There's certainly humor in a function called "safe navigation" mutating the data structure I'm supposedly safely navigating through :D

I don't think this is correct . The name obj gets rebound, but the dictionary being traversed isn't mutated in any way.

Correct. I was drunk.

Re: What's New in Python 3.12

#120

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

On a library level you could return an "empty" object which you could call methods on, returning empty objects and so on, for chaining.

It seems that every language, given enough time, wants to tend towards Haskell!
Post reply on HN