October 23rd, 2013, 04:00 PM
could somebody please explain to me about the function "split"? (on strings)
what is exactly the role of the secend and the third arguments?
October 23rd, 2013, 04:34 PM
I had no idea there were second and third arguments to split....mmm...a third argument anyway. OK, you deserve a real answer not mere snot. I originally intended to say "Try the *!$# thing."
Suppose you use the method directly from a class, as in
Well, it's unbound. There isn't an associated string object. So the first argument needs to be the string to be split.
The second argument is the string used to split the string object.
Apparently there's an optional final integral argument to limit the number of cuts for your experimental fulfillment.
assert str.split('theABcatABate my ABcdefgABdog','AB') == ['the', 'cat', 'ate my ', 'cdefg', 'dog']
But suppose you use OBJECT.method
In this case the method is bound to the OBJECT, silently passed as the first parameter.
Bound versus unbound method call misunderstanding frequently explains programmer's mystification about the strange argument count. Remember, you said str.split takes 3 arguments.
assert 'theABcatABate my ABcdefgABdog'.split('AB') == ['the', 'cat', 'ate my ', 'cdefg', 'dog']
>>> 'the'.split('blort', 'h', 8) # HUH? 2 arguments? I thought there were 3.
Traceback (most recent call last):
File "p.py", line 3, in <module>
'the'.split('blort', 'h', 8)
TypeError: split() takes at most 2 arguments (3 given)
[/code] are essential for python code and Makefiles!
October 23rd, 2013, 09:35 PM
Thank you so much!