Hi folks, I'd like to gather some feedback on a feature suggestion to extend str.partition and bytes.partition to support multiple arguments. Please find the feature described below. # Feature or enhancement The str.partition[1] method -- and similarly the bytes.partition[2] method -- is useful when dividing an input into subcomponents while returning a tuple of deterministic length (currently 3) and potentially retaining the delimiters. The feature described here adds support for multiple positional arguments to those methods. The first positional argument would be applied to the input instance that the method is called from -- no change from existing behaviour. Subsequent positional arguments would be applied to the rightmost component of the previous application of the partition algorithm. So, for example, in pseudocode: ### Single-argument case (matching existing behaviour)
"foo:bar".partition(":") # partition 'foo:bar' using ':' - result = ['foo', ':', 'bar'] # all positional arguments consumed - return result
### Multiple-argument case (added feature)
"foo:bar;baz".partition(":", ";") # partition 'foo:bar;baz' using ':' - result = ['foo', ':', 'bar;baz'] # positional arguments remain; continue # partition 'bar;baz' using ';' - result = ['foo', ':', 'bar', ';', 'baz'] # all positional arguments consumed - return result
# Pitch Multiple-partition-arguments would provide a shorthand / syntactic sugar to abbreviate the process of separating inputs with known-delimeters into components. For example:
login_string = 'username@hostname:2001' username, _, hostname, _, port = login_string.partition('@', ':')
Beneficially for the caller, the number of tuple elements can be determined based on the number of positional arguments. For n arguments, a tuple of length 2n + 1 will be returned. Thank you for any and all feedback. James [1] - https://docs.python.org/3/library/stdtypes.html#str.partition [2] - https://docs.python.org/3/library/stdtypes.html#bytes.partition