Live data from Hacker News

What's New in Python 3.12

docs.python.org

101–110 of 120 posts

Re: What's New in Python 3.12

#101

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?

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

Re: What's New in Python 3.12

#102
post #65

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?

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…

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

Re: What's New in Python 3.12

#104

“The new syntax allows declaring TypeVarTuple and ParamSpec parameters, as well as TypeVar parameters with bounds or constraints:” ``` ... type IntOrStrSequence[T: (int, str)] = Sequence[T] # TypeVar with constraints ``` Why can’t the syntax be ``` type IntOrStrSequence[T: int | str] = Sequence[T] ```

I think because that conflicts with the `bound` syntax which expects a single type.

Re: What's New in Python 3.12

#105
post #65

Earlier quoted context omitted.

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…

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 `=`.

Re: What's New in Python 3.12

#106

“The new syntax allows declaring TypeVarTuple and ParamSpec parameters, as well as TypeVar parameters with bounds or constraints:” ``` ... type IntOrStrSequence[T: (int, str)] = Sequence[T] # TypeVar with constraints ``` Why can’t the syntax be ``` type IntOrStrSequence[T: int | str] = Sequence[T] ```

I think because that conflicts with the `bound` syntax which expects a single type.

Unless I’m mistaken, bound constrains the type to a specific protocol while this constrains the type to specific types.

Re: What's New in Python 3.12

#107

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 don't think this is correct . The name obj gets rebound, but the dictionary being traversed isn't mutated in any way.

Re: What's New in Python 3.12

#108

> Of note, the distutils package has been removed from the standard library. How is this not a major version change if a module is removed from the standard library? Other breaking changes too. This is confusing to me.

It was deprecated in 3.10 and 3.11 with an intent to be removed in 3.12.

Re: What's New in Python 3.12

#109

> Of note, the distutils package has been removed from the standard library. How is this not a major version change if a module is removed from the standard library? Other breaking changes too. This is confusing to me.

Simple: Python does not use semver.

Re: What's New in Python 3.12

#110
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.

Yeah I agree, no mutation here as far as I can tell
Post reply on HN