Show HN: Localscope–Limit scope of Python functions for reproducible execution
Published on: 2025-06-14 01:33:14
Restrict the scope of a callable to local variables to avoid unintentional information ingress.
Parameters : func – Callable whose scope to restrict.
predicate – Predicate to determine whether a global variable is allowed in the scope. Defaults to allow any module.
allowed – Names of globals that are allowed to enter the scope.
allow_closure – Allow access to non-local variables from the enclosing scope.
localscope. mfc # Decorator allowing modules, functions, and classes to enter the local scope.
Examples
Basic example demonstrating the functionality of localscope.
>>> from localscope import localscope >>> >>> a = 'hello world' >>> >>> @localscope ... def print_a (): ... print ( a ) Traceback (most recent call last): ... localscope.LocalscopeException : `a` is not a permitted global (file "...", line 1, in print_a)
The scope of a function can be extended by providing an iterable of allowed variable names or a string of space-separated allowed variable names.
>>> a = 'hello w
... Read full article.