4 ms·
Nice. A class with a single method can be nicely modelled as a closure. Returning higher order functions and using format’s kwargs is an even more succinct ve
by gorgoiler 5y ago
Nice. A class with a single method can be nicely modelled as a closure.
Returning higher order functions and using format’s kwargs is an even more succinct version of this.
def explode(**k)
def run():
return TheBomb(
’{user} is!’.format(**k))
return run
run(user=‘gorgoiler’)
- nerdponx 5y agoI'd almost always rather have a class than a function returning a function, in a language where classes are typical and natural. It imposes more structure on the code, provides some degree of namespacing, and makes it easier to extend.
- gorgoiler 5y agoPersonally, classes feel particularly unnatural in Python. Closures are also weird though, I get your point. I wouldn’t use them in code that’s widely shared. The upside is truly private data.
- deleted 5y ago[deleted]
- iFreilicht 5y agoA class with a single function can also be nicely modelled as a function.
- gorgoiler 5y agoAhem, encapsulation of both data and code :)