In Python 3.13, the behavior of the `locals()` function changed slightly in function scope. The main difference is:
- In Python 3.12 and earlier, `locals()` would return a dict that was a snapshot of the local variables at the time it was called.
- In Python 3.13, `locals()` returns a dict that dynamically reflects changes to local variables.
This change makes the behavior of `locals()` in function scope more consistent with how it already worked in module and class scope.
Here's an example to illustrate the difference:
```python
def example_function():
x = 10
local_vars = locals()
print(f"Initial locals: {local_vars}")
x = 20
print(f"After changing x, locals: {local_vars}")
local_vars['x'] = 30
print(f"After modifying locals dict, x: {x}")
# Python 3.12 output:
# Initial locals: {'x': 10}
# After changing x, locals: {'x': 10}
# After modifying locals dict, x: 20
# Python 3.13 output:
# Initial locals: {'x': 10}
# After changing x, locals: {'x': 20}
# After modifying locals dict, x: 30
```
In this example:
1. In Python 3.12, changing `x` doesn't affect the `local_vars` dict.
2. In Python 3.13, the `local_vars` dict reflects the new value of `x`.
3. In Python 3.12, modifying the `local_vars` dict doesn't affect the actual local variable `x`.
4. In Python 3.13, modifying the `local_vars` dict does change the value of `x`.
This change can be particularly relevant when debugging or when writing introspection tools, as it provides a more accurate and up-to-date view of the local namespace.