News for hackers. Stuff that matters.
Re:What's New in Python 3.12 by JimDabell on Wednesday October 18, 2023 @02:32PM (#37932738)
> 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.

Re:What's New in Python 3.12 by ddejohn on Wednesday October 18, 2023 @02:39PM (#37932857)
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 by woolion on Wednesday October 18, 2023 @05:38PM (#37935125)
The problem was underspecified, but I suggested that under the assumption that the value would be an Optional[dict]. If you have another falsy value, it means either Optional[Any] or something like Optional[Union[dict, list]] (I put list as an example, but you get the idea).

I would say the only reasonable choice is Optional[dict], in which case it should be sufficient, but Python being Python, it could be anything. And in these cases you need to handle all cases way more carefully.