4 ms·
Python tip: instead of making the user contstruct a list literal, just use (star)args in the function definition. Python will automatically pack up any number o
by postfuturist 16y ago
Python tip: instead of making the user contstruct a list literal, just use (star)args in the function definition. Python will automatically pack up any number of positional arguments into a list for you!
def search(*args) : ... # args is a sequence
search(this, that, the_other_thing)
Edit: found the HN FAQ entry on formatting comments, finally.
- andybak 16y agoand you can be extra nice and accept either a list or positional arguments...
- CodeMage 16y agoJust to clarify for anyone who isn't familiar with Python: it's asterisk followed by "args". The problem here is that HN comments system interprets asterisk as italic formatting ;) Edit: Parent comment fixed by postfuturist.
- d0mine 16y agodef search(*args): ...
- troels 16y ago> Edit: found the HN FAQ entry on formatting comments, finally. Really? Where?
- justinl 16y agohttp://news.ycombinator.com/formatdoc http://news.ycombinator.com/formatdoc
- koenigdavidmj 16y agoPython 3 lets you have named keyword arguments too. Anything after the star-args is a named keyword argument, and you can have a simple star if you do not want the argument list: def search(foo, bar, *, baz): Then, foo and bar would be named arguments, and baz would need to be passed by keyword.
- drewolson 16y agoThis is a great point and is something we're planning on adding in an upcoming release of our Python library.