For practice I wrote a function to generate a list of numbers from a start to an end by a step. I couldn't make it- like the built-in range
- default start
to 0, because all optional parameters must come after required ones, so def range(start=0, end, step=1)
won't work.
Here's my script:
def Range(start, end, step=1):
"""Returns a list of numbers beginning from start and
incremented by step to end (with end excluded).
start: an integer specifies the start of the range.
end: an integer specifies the end of the range.
step: an integer specifies the number to increment by.
"""
num = start
result = []
while num < end:
result.append(num)
num += step
return result
How can it be refactored and optimized ?
Notes:
- I'm a hobbyist and beginner (in Python in particular, and programming in general).
1 Answer 1
I'm not sure how the actual python implementation for this works but you could query the length of *args
to assign the parameters to the right variables:
def Range(*args):
len_args = len(args)
if len_args == 2:
args = args + (1,)
elif len_args == 1:
args = (0, args[0], 1)
start, end, step = args
...
In addition I would make this a generator (similar to how it is in python 3.x):
def Range(*args):
len_args = len(args)
if len_args == 2:
args = args + (1,)
elif len_args == 1:
args = (0, args[0], 1)
start, end, step = args
while start < end:
yield start
start += step
-
\$\begingroup\$ That is good! I've thought of using
*args
, but wasn't sure how to make that. Thank you! \$\endgroup\$Mahmood Muhammad Nageeb– Mahmood Muhammad Nageeb2016年09月27日 16:09:33 +00:00Commented Sep 27, 2016 at 16:09 -
\$\begingroup\$ But when doing that how can we make the function usage easy? The user won't know in what order he should put arguments. I think the docstring need to be edited. \$\endgroup\$Mahmood Muhammad Nageeb– Mahmood Muhammad Nageeb2016年09月27日 16:14:46 +00:00Commented Sep 27, 2016 at 16:14
-
2\$\begingroup\$ You can try
def Range(start, stop=None, step=1): if stop is None: start, stop = 0, start
\$\endgroup\$301_Moved_Permanently– 301_Moved_Permanently2016年09月27日 16:16:45 +00:00Commented Sep 27, 2016 at 16:16 -
\$\begingroup\$ I've some questions. Is using
len_args = len(args)
better than usinglen(args)
in each boolean expression? Does that avoid re-computation and make the script faster? \$\endgroup\$Mahmood Muhammad Nageeb– Mahmood Muhammad Nageeb2016年09月27日 17:12:13 +00:00Commented Sep 27, 2016 at 17:12 -
\$\begingroup\$ @Mahmud In theory, yes. in practice
len
is implemented very efficiently and calling it twice will hardly be worth the overhead of saving it in a variable. \$\endgroup\$Graipher– Graipher2016年09月27日 18:02:19 +00:00Commented Sep 27, 2016 at 18:02
Explore related questions
See similar questions with these tags.