4 ms·
> You can say "x = 1 if expr else 2", but you cannot say "x = 1 if expr". You do a two line This one liner works: if expr: x = 1 > Or how about mutable d
by nickles 4y ago
> You can say "x = 1 if expr else 2", but you cannot say "x = 1 if expr". You do a two line
This one liner works:
if expr: x = 1
> Or how about mutable default values in function paramters? That makes a lot of sense...
This can be helpful under certain circumstances (e.g. memoization) [1]. The linked article also points out that it can be useful for rebinding global names in optimized code.
[0] https://stackoverflow.com/a/1145781 https://stackoverflow.com/a/1145781
[1] https://web.archive.org/web/20200221224620id_/http://effbot.org/zone/default-values.htm https://web.archive.org/web/20200221224620id_/http://effbot....
- z9znz 4y ago> if expr: x = 1 But PEP8 and now the all-too-popular Black formatter disallow this. I think it's fine. The momoization/optimization point is exactly the opposite of what should be a default use case for a beginner friendly language. And I highly doubt this mutable default design was intentional for these purposes. They are footguns.