4 ms·
Python doesn't have multiline lambdas, so there is no way to create an api similar to rake's: task :name do |t| # do stuff end I'm not sure what's the
by arockwell 17y ago
Python doesn't have multiline lambdas, so there is no way to create an api similar to rake's:
task :name do |t|
# do stuff
end
I'm not sure what's the closest equivalent syntax you could create in python.
- tsuraan 17y agoPython's answer to blocks tends to be the with statement, so your example would look like with task('name') as t: # do stuff with t It's not really the same; the task object doesn't have control over whether the block is actually run, but sometimes it is sufficient.
- fuzzyman 17y agoYou could almost certainly achieve that with the implementation of blocks provided by this package: http://pypi.python.org/pypi/withhacks/ http://pypi.python.org/pypi/withhacks/
- bad_user 17y agothat's cool, thanks ... I was wondering about that
- tsuraan 17y agoWow, I've never seen that before. I'm also not sure I'd say a solution that uses bytecode rewriting is really working within the language, but that's a pretty interesting package anyhow.
- fuzzyman 17y agoWell, normally I would agree - but if its entirely abstracted away in a library (and the library is well written) then it might sort-of-kind-of-maybe be ok. Python itself does varying degrees of bytecode optimisation depending on which version you're using.
- viraptor 17y agoNot sure what this snippet does, but if it creates a block that is registered as a task named "name", then you can do the same with decorators: def task(f): register_task(f.__name__, f) return f @task def name(t) ..... or similar.
- algorias 17y agoExactly. I fail to understand why people are so dismissive of languages/approaches that do things slightly differently and don't implement equivalent things in the exact same way. What's so special about the rake syntax?
- tghw 17y agoWhy do you need lambdas to do this? Why wouldn't an inline function work?
- grandalf 17y agoThis is correct, it would work. People who harp on this probably don't actually use Python for anything.
- jashkenas 17y agoAnother CoffeeScript snippet, if you're working with Node.js or the like: task 'name', (t) -> # do multiline stuff