[issue34169] itertools.repeat does not accept None as an explicit count

2018-07-20 Thread Clint Hepner


New submission from Clint Hepner :

I expected to be able to pass None as an explicit count to itertools.repeat to 
get the default behavior of an infinite iterator:

>>> list(islice(repeat(1), 10))
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
>>> list(islice(repeat(1, None), 10))
Traceback (most recent call last):
  File "", line 1 in 
TypeError: 'NoneType' object cannot be interpreted as an integer

--
components: Library (Lib)
messages: 322010
nosy: chepner
priority: normal
severity: normal
status: open
title: itertools.repeat does not accept None as an explicit count
versions: Python 3.7

___
Python tracker 
<https://bugs.python.org/issue34169>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34169] itertools.repeat does not accept None as an explicit count

2018-07-21 Thread Clint Hepner


Clint Hepner  added the comment:

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.

--

___
Python tracker 
<https://bugs.python.org/issue34169>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue22138] patch.object doesn't restore function defaults

2014-08-04 Thread Clint Hepner

New submission from Clint Hepner:

Following a patch, a function's __defaults__ attribute is reset to None.

def foo(x=5):
return x

assert foo() == 5  # As expected
with unittest.mock.patch.object(foo, '__defaults__', (10,)):
assert foo() == 10  # As expected

assert foo() == 5  # Fails
assert foo.__defaults__ is None  # Succeeds

--
components: Library (Lib)
messages: 224801
nosy: chepner
priority: normal
severity: normal
status: open
title: patch.object doesn't restore function defaults
type: behavior
versions: Python 3.4

___
Python tracker 
<http://bugs.python.org/issue22138>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com