In Python, what is the reason for "overwriting" the default 0 argument?
I am reading the source code of the Scrapy-Redis example project, https://github.com/rolando/scrapy-redis/blob/master/example-project/process_items.py , which contains the following:
def process_items(r, keys, timeout, limit=0, log_every=1000, wait=.1):
limit = limit or float('inf')
It seems to me that the de facto default for limit
- float('inf')
, because if not specified, it first takes on the default 0
and then is immediately converted to float('inf')
in the string containing the boolean operator or
.
My question is, why not just set the default limit=float('inf')
on the string def
? Is there a reason for this pattern?
source to share
The code is driven by the command line interface. The command line interface uses 0
the default for limit
. This is the de facto standard for disabling limits in such cases, since using other options (instead of using the command line user float('inf')
or a separate switch that disables the limit again if set in other ways) becomes unnecessarily complicated.
The function then uses the same default; presumably to make it easier to use this feature outside of the command line use case (by importing the script), making it fairly self-documenting. What the actual code uses float('inf')
is instead an implementation detail.
There is no technical reason to use default=0
more default=float('inf')
.
source to share