4 ms·
Python has had closures for a long time, which make great ad-hoc factories that can capture state from upstream functions without making state visible along the
by dom0 9y ago
Python has had closures for a long time, which make great ad-hoc factories that can capture state from upstream functions without making state visible along the way.
- chubot 9y agoYeah that can probably help. Another thing that helps is keyword arguments, so you avoid the verbose builder pattern that's idiomatic in Java. I was going to say that I don't use closures in Python all that much, and Python has some issues with closures (e.g. nonlocal is ugly, and it wasn't even there until Python 3). But after watching a recent PyCon talk [1], I realized that the "bound method" feature of Python is a useful special case of a closure. And he makes the good point that the code is shorter than JavaScript which requires an explicit anonymous function or bind(). [1] https://us.pycon.org/2017/schedule/presentation/768/ https://us.pycon.org/2017/schedule/presentation/768/