From 0cfde115ffc2da29dd1b7503c0d0d1368185fc53 Mon Sep 17 00:00:00 2001 From: Mark Zhitomirski Date: Fri, 31 Oct 2025 00:13:17 +0400 Subject: [PATCH] fix ValueError in f_locals.pop('self') Python 3.13+ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The error ValueError: cannot remove local variables from FrameLocalsProxy is caused by PEP 667 – Consistent views of namespaces peps.python.org/pep-0667 implemented in Python 3.13 --- Exercises/ex6_2.md | 9 ++++++--- Exercises/soln6_2.md | 3 ++- Exercises/soln6_3.md | 3 ++- Solutions/6_2/structure.py | 3 ++- Solutions/6_3/structure.py | 3 ++- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Exercises/ex6_2.md b/Exercises/ex6_2.md index cd90e833..93ba28fd 100644 --- a/Exercises/ex6_2.md +++ b/Exercises/ex6_2.md @@ -61,8 +61,9 @@ and class definitions: ```python >>> def _init(locs): - self = locs.pop('self') + self = locs['self'] for name, val in locs.items(): + if name == 'self': continue setattr(self, name, val) >>> class Stock: @@ -96,8 +97,9 @@ frame hacking. Try this variant of the `_init()` function: >>> import sys >>> def _init(): locs = sys._getframe(1).f_locals # Get callers local variables - self = locs.pop('self') + self = locs['self'] for name, val in locs.items(): + if name == 'self': continue setattr(self, name, val) >>> ``` @@ -135,8 +137,9 @@ class Structure: @staticmethod def _init(): locs = sys._getframe(1).f_locals - self = locs.pop('self') + self = locs['self'] for name, val in locs.items(): + if name == 'self': continue setattr(self, name, val) ... ``` diff --git a/Exercises/soln6_2.md b/Exercises/soln6_2.md index 1f4dc4eb..476c1c01 100644 --- a/Exercises/soln6_2.md +++ b/Exercises/soln6_2.md @@ -11,8 +11,9 @@ class Structure: @staticmethod def _init(): locs = sys._getframe(1).f_locals - self = locs.pop('self') + self = locs['self'] for name, val in locs.items(): + if name == 'self': continue setattr(self, name, val) def __setattr__(self, name, value): diff --git a/Exercises/soln6_3.md b/Exercises/soln6_3.md index 2633d014..84864e95 100644 --- a/Exercises/soln6_3.md +++ b/Exercises/soln6_3.md @@ -12,8 +12,9 @@ class Structure: @staticmethod def _init(): locs = sys._getframe(1).f_locals - self = locs.pop('self') + self = locs['self'] for name, val in locs.items(): + if name == 'self': continue setattr(self, name, val) def __setattr__(self, name, value): diff --git a/Solutions/6_2/structure.py b/Solutions/6_2/structure.py index fb485252..c925b160 100644 --- a/Solutions/6_2/structure.py +++ b/Solutions/6_2/structure.py @@ -8,8 +8,9 @@ class Structure: @staticmethod def _init(): locs = sys._getframe(1).f_locals - self = locs.pop('self') + self = locs['self'] for name, val in locs.items(): + if name == 'self': continue setattr(self, name, val) def __setattr__(self, name, value): diff --git a/Solutions/6_3/structure.py b/Solutions/6_3/structure.py index 55b08ce3..bec51990 100644 --- a/Solutions/6_3/structure.py +++ b/Solutions/6_3/structure.py @@ -9,8 +9,9 @@ class Structure: @staticmethod def _init(): locs = sys._getframe(1).f_locals - self = locs.pop('self') + self = locs['self'] for name, val in locs.items(): + if name == 'self': continue setattr(self, name, val) def __setattr__(self, name, value):