Say you have a function that needs to maintain some sort of state and behave differently depending on that state. I am aware of two ways to implement this where the state is stored entirely by the function:

1. Using a function attribute
2. Using a mutable default value

Using a slightly modified version of [Felix Klings answer to another question](https://stackoverflow.com/questions/7182546/how-to-replace-the-nth-appearance-of-a-needle-in-a-haystack-python/7182712#7182712), here is an example function that can be used in `re.sub()` so that only the third match to a regex will be replaced:

Function attribute:

```
def replace(match):
    replace.c = getattr(replace, "c", 0) + 1
    return repl if replace.c == 3 else match.group(0)

```

Mutable default value:

```
def replace(match, c=[0]):
    c[0] += 1
    return repl if c[0] == 3 else match.group(0)

```

To me the first seems cleaner, but I have seen the second more commonly. Which is preferable and why?