I'm trying to write a program which finds primes in a certain range. One of the things which I'm trying to do is allow the function to be called without all its arguments.
What I want to do is something like this:
def find_primes(start=1, stop, primes=None):
The primes variable will then be initialised to a blank list (I'm trying to make the program recursive).
However, this will cause an error because I cannot use a default value for an argument before all the required values.
One way I thought of doing this was:
def find_primes(start, stop=-1, primes=None):
if primes is None:
primes = []
if stop = -1:
stop = start
start = 1
Basically, I can flip the variables if stop remained at its default, out-of-range value. However this seems quite hacky and I am hoping there is a better way of doing this.
An example of somewhere I know this is implemented is the range function, because I can call it as
range(stop)
or
range(start, stop[, step])
Is this possible to implement? Thanks in advance.
EDIT: In other languages, I could use function overloading:
def find_primes(stop):
return find_primes(1, stop)
def find_primes(start, stop, primes=None)
#Code
Does this exist in Python?