[Python-ideas] Dart-like method cascading operator in Python
Perešíni Peter
ppershing at gmail.com
Thu Nov 21 15:08:20 CET 2013
On Thu, Nov 21, 2013 at 2:34 PM, Masklinn <masklinn at masklinn.net> wrote:
> On 2013-11-21, at 13:40 , Nick Coghlan <ncoghlan at gmail.com> wrote:
>
> > On 21 November 2013 20:55, Perešíni Peter <ppershing at gmail.com> wrote:
> >> gnuplot.newPlot()
> >> ..set("xrange [0:5]")
> >> ..set("yrange [0:20]")
> >> ..newPlot()
> >> ..addSeries("Linear", [1,2,3])
> >> ..addSeries("Quadratic", [1,4,6])
> >> ..run()
> >
> > If you just want structural grouping of some code, you can already
> > define an appropriate context manager:
> >
> > @contextlib.contextmanager
> > def value(x)
> > yield x
> >
> > with value(gnuplot.newPlot()) as p:
> > p.set("xrange [0:5]")
> > p.set("yrange [0:20]")
> > with value(p.newPlot()) as n:
> > n.addSeries("Linear", [1,2,3])
> > n.addSeries("Quadratic", [1,4,6])
> > p.run()
> >
> > It doesn’t define a new scope
>
> And it requires naming things.
>
Exactly, point of the proposal is to avoid repetitive naming
>
> An other drawback is that it is a statement, where the cascading
> operator yields an expression (at least in Smalltalk it did) (well of
> course more or less everything was an expression in smalltalk so that
> helped).
>
> I really liked message cascading when I played with it in Smalltalk, but:
>
> 1. I find Dart’s cascading syntax rather noisy, and examples on The
> Internets seem to show it regularly used in single-line expression
> which I find a detriment to readability:
>
> document.body.children.add(new
> ButtonElement()..id='awesome'..text='Click Me!’;);
>
Agree, this example is extra bad and unreadable. If we want cascading (and
especially nested cascading), I would force cascading operator to be the
first token on a new (and indented) line as in my examples
>
> 2. Unless I missed it, the original suggestion failed to specify what
> the overall expression returns. In Smalltalk, it returns the value of
> the last message of the cascade, and Smalltalk has a `yourself` message
> which returns, well, its self. IIRC, Python has no such message “built
> in”.
>
>
It would return the result of the expression before cascading, e.g.
o = MyObject()
..set(x)
..set(y)
is a syntactic sugar for
tmp = MyObject()
tmp.set(x)
tmp.set(y)
o = tmp
Note that we need a get the priority right though, e.g. cascading operator
takes precedence over assignment in order to avoid surprises
3. It encourages and facilitates APIs based on object mutation and
> incompletely initialised objects, the former being a slight dislike and
> the latter being something I loathe.
>
>
Agree. It may lead to a bit more sloppier API design than usual.
> 4. I doubt OP’s (a) would be fixed, it’s not an issue of attribute deref,
> so
> a cascading operator would have the exact same behaviour.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131121/ae7405af/attachment-0001.html>
More information about the Python-ideas
mailing list