[Python-ideas] Method chaining notation

Chris Angelico rosuav at gmail.com
Mon Feb 24 14:48:54 CET 2014


On Tue, Feb 25, 2014 at 12:10 AM, Andrew Barnert <abarnert at yahoo.com> wrote:
> So I put together examples for both the PyGtk example that started this thread and the Java example that started the whole fluent-interface fad, along with an explanation of why method chaining is neither necessary in, nor a good fit for, Python even though it's very useful in languages like Java. See https://stupid-python-ideas.runkite.com/fluent-pythonic/ for the whole thing.
>

While I do think a method chaining operator would solve the problem
generically, rather than requiring every module to do it individually,
I do rather like your proposed window creation syntax. The thing is,
Pike GTK is *almost* there: instead of creating a window and setting
its title and border, you can create a window and pass it a mapping
(dict) specifying the title and border. (PyGTK doesn't have anything
of the sort, it seems. I haven't looked into PyGObject, which is
supposed to be the new great thing; it might have that.) But it's not
quite all the way, because you can't stuff children into them.

Being able to do the whole job in the constructor is extremely
tempting. It'd work beautifully for the objects where you basically
just call add() with each thing (just provide a list of children to be
added), but not so well when you want to specify parameters (eg
specifying how spare space should be allocated). I'm not sure how that
ought to be done. I could come up with something where you pass it a
list of tuples, but I'm not sure that completely covers the issue
either. There's no perfect solution, which is why the search
continues.

I freely admit that the suggestion I made at the beginning of this
thread is unideal; it's un-Pythonic, it's a heavy language change, and
it'd take a huge amount of justification to go anywhere. But there is
a problem still, that it's trying to solve.

ChrisA


More information about the Python-ideas mailing list