Make all builtin functions accept None for optional arguments
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`? Thanks, Ram.
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))
Yes, this is a consequence of having to currently write the argument parsing code for builtins by hand (and a lot of those implementations predating the evolution of more modern conventions). It's likely to change in the future, as Argument Clinic makes such things easier to maintain and make consistent. Cheers, Nick.
On 2/4/2014 5:45 AM, 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.
As MRAB pointed out, count does have a default
help(str.rsplit) Help on method_descriptor:
rsplit(...) S.rsplit(sep=None, maxsplit=-1) -> list of strings But there are builtins with optional parameters with no default, and they *are* annoying because they do need conditional code like the above to not pass any value. Some developers would like to add None as default for such cases. For reasons explained by Nick, at least some will in 3.5. Replacing (or augmenting) -1 with None when -1 means None is trickier because of back compatibility. Another annoyance for builtins is the lack of documentation as to which parameters can only be passed by position and not by keyword. You cannot be sure from just reading the above that ''.rsplit(maxsplit = 3) is even legal. This should also be fixed in 3.5. -- Terry Jan Reedy
participants (4)
-
MRAB
-
Nick Coghlan
-
Ram Rachum
-
Terry Reedy