[Python-ideas] Dart-like method cascading operator in Python

Masklinn masklinn at masklinn.net
Sat Nov 23 12:53:08 CET 2013


On 2013-11-23, at 12:17 , Stephen J. Turnbull <stephen at xemacs.org> wrote:

> Perešíni Peter writes:
> 
>> Anything which is tree-like (e.g. mentioned DSLs, API wrappers
>> around complicated objects (e.g. xml), configurations, gnuplot
>> example, ...) can be difficult to instantiate using normal syntax:
> 
>> tree = Tree()
>>   ..newChild(value=5)
>>       ..newChild(value=3)
>>       ..newChild()
>>   ..newChild(color="green")
> 
> This particular application just screams for positional parameters,
> though:
> 
>    tree = Tree(
>                Node(
>                     Node(value=3),
>                     Node(),
>                     value=5),
>                Node(color="green"))
> 
> (YMMV about positioning of the parens, to me this is the one that says
> "TREE!! and here are the Nodes” most clearly.)

Doesn’t work on third-party libraries on which you don’t have
control[0] though, and when there are many customisation points having
subsequent methods can make for a nicer API than 50 constructor
parameters[1].

[0] such as the standard library’s, unless I am mistaken neither
    minidom nor elementtree allow complete element configuration —
    including subtree — in a single expression. The third-party lxml does
    to an extent, if one uses the builder APIs rather than the ET ones.

[1] or the existing API may make it difficult to impossible to add such
    an extension without breaking


More information about the Python-ideas mailing list