Message322099
This came up in response to https://stackoverflow.com/q/51443795/1126841.
I realize the current documentation is informative, not normative, but I think
there is a legitimate reason to allow an explicit None argument like the pure Python suggests.
Currently, there is no way to trigger the default behavior explicitly, as repeat behaves more like iter (in that the number of arguments has special meaning) than like islice (whose optional arguments behave like regular parameters with default values).
For example, these two calls are equivalent:
islice(itr, 5)
islice(itr, 5, None)
For some functions, it makes sense for the number of arguments to be significant. For example, there is no meaningful default value for the second argument to iter(), since the second argument completely changes the semantics of the call.
iter({'a': 1, 'b': 2}) # Return an iterator for the argument
iter(lambda f: f.read(4), '') # Call a function until it returns ''
As an another example, the first argument to map() determines how many
additional arguments are needed.
map(lambda x: x + 1, [1,2,3]) # yield values form [2,3,4]
map(lambda x: x + y, [1,2,3], [1,2,3]) # yield values from [2,4,6]
However, with repeat(), it makes sense to think of the second argument as an upper limit that can be either finite or infinite. Lacking an infinite integer value, None makes sense if you read it as "no upper limit"
repeat(1) # infinite stream of 1s
repeat(1, 10322) # a finite stream
repeat(1, None) # proposed: an infinite stream of 1s
I prefer the current description of
def repeat(object, times=None):
over
def repeat(object, *times)
because it accurately reflects the number of arguments you can pass to repeat. |
|
Date |
User |
Action |
Args |
2018-07-21 14:10:32 | chepner | set | recipients:
+ chepner, rhettinger, terry.reedy, corona10 |
2018-07-21 14:10:32 | chepner | set | messageid: <1532182232.62.0.56676864532.issue34169@psf.upfronthosting.co.za> |
2018-07-21 14:10:32 | chepner | link | issue34169 messages |
2018-07-21 14:10:32 | chepner | create | |
|