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

Nick Coghlan ncoghlan at gmail.com
Thu Nov 21 13:40:41 CET 2013


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, but it can sometimes help break up a
large block of initialisation code (although often a helper function
or two may be a better way to do that).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list