Crazy what-if idea for function/method calling syntax

Pierre Quentel pierre.quentel at gmail.com
Mon Jul 18 16:34:49 EDT 2011


On 18 juil, 07:54, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> On Mon, 18 Jul 2011 08:54 am ΤΖΩΤΖΙΟΥ wrote:
>
> > Jumping in:
>
> > What if a construct
>
> >    xx(*args1, **kwargs1)yy(*args2, **kwargs2)
>
> > was interpreted as
>
> >   xxyy(*(args1+args2), **(kwargs1+kwargs2))
>
> > (Note: with **(kwargs1+kwargs2) I mean “put keyword arguments in the
> > order given”, since dicts can't be added)
>
> > This construct is currently a syntax error. The intent of this idea is
> > to help improve legibility.
>
> I don't think it does that. I think it is misleading, as it looks like two
> independent function calls. It also makes it hard to search for a function
> call -- instead of searching for
>
> do_something\(.*\)
>
> you have to now search for
>
> do_something\(.*\)
> do\(.*\)_something\(.*\)
> do_\(.*\)something\(.*\)
> do_some\(.*\)thing\(.*\)
>
> and so on.
>
> > Example:
> >   def place_at(item, x, y): blah blah
> > could be called as
> >   place(item)_at(x, y)
>
> You would probably like the Xtalk family of languages, starting with
> Hypertalk from Apple in the late 80s or early 90s.
>
> There's a neat implementation here:http://code.google.com/p/openxion/
>
> Xtalk includes syntax like this:
>
> put newStr into character 23 to 42 of theStr
> put suffix after theStr
> delete first char of theStr
>
> although this only applied to built-in functions, not user-functions.
>
> --
> Steven

If I understand correctly, you propose to translate "do something to X
with arguments a,b,c" to

(1) do_something_to(X)_with_arguments(a,b,c)

instead of

(2) do_something(X,a,b,c)

I agree that the first one is more readable than the second, because
in the arguments list in (2) you mix the object you are working on and
the parameters used. But there is another option :

(3) X.do_something_with_arguments(a,b,c)

which would be in your examples : "item.place_at(x,y)" or
"iterable.group_by(collection)"

It's valid Python code and probably as readable than what you suggest,
with a clear distinction between the object and the arguments

- Pierre



More information about the Python-list mailing list