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

Terry Reedy tjreedy at udel.edu
Fri Nov 22 04:29:20 CET 2013


On 11/21/2013 9:16 PM, Steven D'Aprano wrote:
> On Thu, Nov 21, 2013 at 11:55:46AM +0100, Perešíni Peter wrote:
> I don't see how this syntax can simplify instantiating
> objects:
>
> obj = MyClass("spam", "eggs")
>
> is already pretty simple. Perhaps you're referring only to a small
> subset of (in my opinion) *poorly designed* if not outright buggy
> objects which aren't instantiated completely on creation and need to be
> tweaked by hand before being ready to use. Or those with excessively
> complicated APIs that could really do with a few helper functions.
>
> In my opinion, we shouldn't encourage classes that require manual
> instantiation like this:
>
> obj = MyClass()
> obj.foo = "spam"
> obj.setbar("eggs")
> obj.make_it_work()
> # now obj is fully instantiated and ready to be used...
>
> not even with your suggested syntax:
>
> obj = MyClass()
>    ..foo = "spam"
>    ..setbar("eggs")
>    ..make_it_work()
>
>
> Helping people do the wrong thing is not, in my opinion, an advantage.

I had the same thought.

> The use-case of DSLs is perhaps more interesting, but a simple example
> would go a long way to support the idea.
>
>
>> In particular, we can
>> make this much more powerful in Python (as opposed to Dart) because Python
>> recognizes scope by indentation and therefore it would be possible to do
>> something like
>>
>> gnuplot.newPlot()
>>   ..set("xrange [0:5]")
>>   ..set("yrange [0:20]")
>>   ..newPlot()
>>       ..addSeries("Linear", [1,2,3])
>>       ..addSeries("Quadratic", [1,4,6])
>>   ..run()
>
> I had to read that multiple times before I was able to interpret what
> this is supposed to mean. It doesn't help that I'm not familiar enough
> with the gnuplot API to tell exactly what you're doing. I *think* that
> it would be the equivalent of this:
>
> p = gnuplot.newPlot()
> p.set("xrange [0:5]")
> p.set("yrange [0:20]")
> q = p.newPlot()
> q.addSeries("Linear", [1,2,3])
> q.addSeries("Quadratic", [1,4,6])
> p.run()

which I would want to at least partially be equivalent to something like

p = gnuplot.newPlot(xrange=(0,5), yrange = (0,20)
q = p.newplot(series = (("linear", [1,2,3]), ("quadratic", [1,4,6])))
p.run()

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list