Implicit vs. explicit sequences in calls

16 views
Skip to first unread message

Stefan Schwarzer

unread,
Aug 3, 2010, 5:03:40 AM8/3/10
to python-csp
Hello,

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

Fabian

unread,
Aug 5, 2010, 2:03:20 PM8/5/10
to python-csp
Ehm,

I agree in all points!

Sarah Mount

unread,
Aug 7, 2010, 10:11:05 AM8/7/10
to pytho...@googlegroups.com
On 5 August 2010 19:03, Fabian <fabian...@gmail.com> wrote:
> Ehm,
>
> I agree in all points!
>

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

Reply all
Reply to author
Forward
0 new messages