You do so by not using recursion; you'd run into the recursion limit for circles with a radius matching the recursion limit, which defaults to 1000, and the limit can't arbitrarily be raised. Use an iterative approach instead. You can do so here with a queue:
from collections import deque
def floodfill(x, y, _directions=((-1, 0), (0, -1), (1, 0), (0, 1))):
queue = deque([(r, c)])
handled = {(r, c)}
while queue:
r, c = queue.popleft()
for dr, dc in _directions:
nr, nc = r + dr, c + dc
if (nr, nc) not in handled:
handled.add((nr, nc))
queue.append((nr, nc))
# do something with these coordinates, like filling
# this position in an image.
I used a set to track what coordinates have not yet been handled here; there may be simpler way for your application to do the same (like simply testing that the floodfill colour is already applied to that pixel).
You also would test for the boundary conditions in the same if test location. If the pixel is already black there, you'd also ignore those coordinates.
floodfill(1, 1)this will callfloodfill(2, 1)which will callfloodfill(1, 1)which will... If you have no infinite recursion either try raising the recursion limit or implement it in a more direct way, i.e. instead of calling the function write the arguments in a list and usewhile list: args = list.pop(); f(*args)or something like that (which is basically dynamic programming).floodfill?