
On Tue, May 02, 2017 at 09:07:41PM -0400, Juancarlo AƱez wrote:
On Tue, May 2, 2017 at 8:43 PM, Steven D'Aprano <steve@pearwood.info> wrote:
String methods should return strings.
"A-B-C".split("-") ['A', 'B', 'C']
Yes, thank you. And don't forget: py> 'abcd'.index('c') 2 But in context, I was responding to the question of why this proposed chunk()/group() method returns a string rather than an iterator. I worded my answer badly, but the intention was clear, at least in my own head *wink* Given that we were discussing a method that both groups the characters of a string and inserts the separators, it makes sense to return a string, like other string methods: 'foo'.upper() returns 'FOO', not iter(['F', 'O', 'O']) 'cheese and green eggs'.replace('green', 'red') returns a string, not iter(['cheese and ', 'red', ' eggs']) 'xyz'.zfill(5) returns '00xyz' not iter(['00', 'xyz']) etc, and likewise: 'abcdef'.chunk(2, sep='-') should return 'ab-cd-ef' rather than iter(['ab', '-', 'cd', '-', 'ef']) If we're talking about a different API, one where only the grouping is done and inserting separators is left for join(), then my answer will be different. In that case, then it is a matter of taste whether to return a list (like split()) or an iterator. I lean slightly towards returning a list, but I can see arguments for and against both. -- Steve