How do I chain methods?

Stefan Schwarzer sschwarzer at sschwarzer.net
Mon Oct 25 07:20:21 EDT 2010


Hi Steve and others,

On 2010-10-25 06:08, Steve Holden wrote:
> On 10/24/2010 11:42 PM, Chris Rebert wrote:
>> On Sun, Oct 24, 2010 at 4:11 PM, James Mills
>> <prologic at shortcircuit.net.au> wrote:
>>> I don't agree but anyway... I've just not seen it commonly used
>>> amongst python programmers.
>>
>> If Python wanted to encourage method-chaining-style, then list.sort(),
>> list.reverse(), and several other built-in type methods would (ala
>> Ruby) return self rather than None. Since they don't, and since
>> "uncommon idiom" is a near-oxymoron, I think we can safely conclude
>> that method chaining isn't idiomatic in Python. Not that it doesn't
>> have specialized uses though (See asterisk note).
>>
> Yes, the Twisted guys use method chaining a lot - it's definitely
> idiomatic in that framework.

It's also used in the pstats module:
http://docs.python.org/library/profile.html#instant-user-s-manual

But I agree with others that it doesn't seem to be a
typical Python idiom.

Stefan



More information about the Python-list mailing list