[Python-ideas] Make all builtin functions accept None for optional arguments
MRAB
python at mrabarnett.plus.com
Tue Feb 4 16:20:52 CET 2014
On 2014-02-04 10:45, Ram Rachum wrote:
> Here is something that always annoys me.
>
> I was going to write my own rreplace function, like this:
>
> def rreplace(s, old, new, count=None):
> return new.join(s.rsplit(old, count))
>
>
> But lo and behold, I have to write it like this:
>
> def rreplace(s, old, new, count=None):
> return new.join(s.rsplit(old, count) if count is not None
> else s.rsplit(old))
>
>
> Why? Because the `str.rsplit` can't handle a count of `None`. That is
> quite annoying.
>
> There are many more builtin functions except `str.rsplit` that behave
> like this.
>
> What do you think about going over all such functions and making them
> able to accept `None`?
>
It accepts -1:
def rreplace(s, old, new, count=-1):
return new.join(s.rsplit(old, count))
More information about the Python-ideas
mailing list