4 ms·
Where I've found `?` super helpful in JS/TS and now miss it the most in Python is dealing with nested data structures. ``` if (foo.bar?.baz?.[5]?.bazinga?.valu
by Swizec 2y ago
Where I've found `?` super helpful in JS/TS and now miss it the most in Python is dealing with nested data structures.
```
if (foo.bar?.baz?.[5]?.bazinga?.value)
```
Is so much nicer than
```
if foo.bar and foo.bar.baz and foo.bar.baz[5] and foo.bar.baz[5].bazinga and foo.bar.baz[5].bazinga.value
```
I honestly don't care which one of those is falsy, my logic is the same either way.
This enables you to ergonomically pass around meaningful domain-oriented objects, which is nice.
Edit: looks like optional chaining is a separate proposal – https://github.com/golang/go/issues/42847 https://github.com/golang/go/issues/42847
- nonethewiser 2y agoYes, same experience. As someone who learned on python then got deep into Typsecript, this was a major bummer when returning to python. Its funny because when I was a beginner in both I strongly preferred python syntax. I thought it was much simpler.
- new_user_final 2y agoAs I have pointed in in my other comment, you can use following syntax in python, ugly but better than multiple and (I don't use python anymore) if dict.get('key', {}).get('key-nested',{}).get....:
- jon_richards 2y agoEven less pretty for getattr. And then you have a rude awakening about the difference between the missing default behavior of these two functions.
- new_user_final 2y agoYou can use following syntax in python, ugly but better than multiple and (I don't use python anymore) if dict.get('key', {}).get('key-nested',{}).get....: