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?
What's New in Python 3.12
101–110 of 120 posts
Re: What's New in Python 3.12
#102Earlier 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…
Re: What's New in Python 3.12
#103How 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.
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] ```
Re: What's New in Python 3.12
#105Earlier 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 :)
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.
Re: What's New in Python 3.12
#107Earlier 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
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.
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.
Re: What's New in Python 3.12
#110Earlier 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.