Here's something that's been on my mind for a while.
Currently, there are several places in the Python-CSP code
that work on implicit sequences, for example
processes = csp.Par(process1, process2, process3)
Some thoughts on this:
- Using an explicit sequence instead of writing just the
arguments is a more explicit API. I prefer to write
processes = csp.Par([process1, process2, process3])
instead of the current form, even if the list brackets add
a bit of "noise".
- Using an explicit sequence makes it unnecessary to convert
arguments back and forth with `*seq` syntax. (In my
opinion, frequent use of `*seq` gives a hint that the API
should use an explicit sequence in the first place because
it's more natural.)
- The implicit sequence arguments get you in trouble as soon
as you need another argument as in
processes = csp.Par(process1, process2, process3,
another_argument_which_isnt_a_process)
In that case you need awkward logic to process the
arguments correctly. You can alleviate this a bit by
requiring the additional arguments to be keyword arguments
(as in the Python builtins `min` and `max`), but I think
that's more of a crutch than elegance.
- Using an explicit sequence makes it easy to extend the API
to use arbitrary iterables. Imagine you have a generator
which yields processes. In the current API you would have
to write.
iterable = my_generator(...)
processes = csp.Par(*list(iterable))
If you're not sure if `*` or the `list` call has higher
priority, you'd even have to use another pair of
parentheses:
iterable = my_generator(...)
processes = csp.Par(*(list(iterable)))
In the case of a general iterable as an argument, you write
iterable = my_generator(...)
processes = csp.Par(iterable)
I hope these points are convincing enough to change the API
to require explicit sequences instead of implicit ones. :-)
Stefan
Thanks both. I've been in two minds about this ever since I read the
thread a few days ago. I like the original syntax, it's more DSL-ish,
but your argument about iterables is a good one; so if more people
think it's more Pythonic to make the list explicit we should probably
do that. I've added this as Issue 47 on the code repository.
Thanks,
Sarah
--
Sarah Mount, Senior Lecturer, University of Wolverhampton
website: http://www.snim2.org/
twitter: @snim2