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.
What's New in Python 3.12
61–70 of 120 posts
Re: What's New in Python 3.12
#62PEP 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…
Re: What's New in Python 3.12
#63I 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
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 NoneRe: What's New in Python 3.12
#64Re: What's New in Python 3.12
#65I 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?
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
#66PEP 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…
Is date a string or an object? It gets tiresome.
Re: What's New in Python 3.12
#67It has become an unpleasant chore to keep up with PEP squabbles and accepted changes.
Re: What's New in Python 3.12
#68What 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…
Re: What's New in Python 3.12
#69PEP 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…
Re: What's New in Python 3.12
#70I 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.