Skip to main content

Dynamic Scope in Python

This module implements dynamic scope for Python: it allows Python code to get and set variables defined anywhere in its call stack, searching first its own local variables, then its caller’s local variables, and so on, until it finds a variable with the desired name.

It is strongly recommended to only use this module for education, fun party tricks, and write-only throw-away code which no one will ever need to reuse or maintain.

Versioning

This library’s version numbers follow the SemVer 2.0.0 specification.

Installation

pip install dynamicscope

Usage

Import:

from dynamicscope import DYNAMIC_SCOPE

To get a variable named x in the current dynamic scope:

DYNAMIC_SCOPE.x

(Note: dynamic scope starts with your stack frame, not your caller’s stack frame, so if you have a variable named x in the local scope, DYNAMIC_SCOPE.x will refer to that x, not bypass it.)

To overwrite a existing variable named x in the current dynamic scope:

DYNAMIC_SCOPE.x = 'foo'

To delete a variable named x found in the current dynamic scope:

del DYNAMIC_SCOPE.x

(DANGER: reaching up from a lower scope to delete variables in callers is a GOTO-level crime under international software law.)

If a variable is not found in dynamic scope, an AttributeError is raised:

AttributeError: 'x' not found in dynamic scope

(It is an AttributeError and not a NameError because dynamic scope is accessed with attribute access - returning the standard error for attribute access composes better with existing code and builtins like hasattr, getattr, and delattr.)

Getting and setting variables with dynamic scope is not always possible in Python. Two errors, both subclasses of RuntimeError, are defined to help you detect when it cannot be done:

  • If the stack cannot be inspected to find variables during either a get, set, or delete: a dynamicscope.ReadError is raised.

  • If the stack cannot be modified during a set or delete: a dynamicscope.WriteError is raised.

Caution

Most languages do not have dynamic scope for really good reason.

In a world where dynamic scope is used, your local variables are part of your observable interface, and the only thing protecting two pieces of code from trampling over or shadowing each others’ variables is conventions about how things are named. For example:

  • {{your module name}}_foo for any variable that you intend for your users to set or get through dynamic scope, and

  • _{{your module name}}_foo for any variable that you intend for your code to privately set or get through dynamic scope.

Details

dynamicscope works by using Python’s stack frame inspection, specifically inspect.currentframe() and the f_locals attribute of frame objects.

Metadata

Release files for dynamicscope 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dynamicscope 1.0.4
File Size Uploaded
dynamicscope-1.0.4.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dynamicscope 1.0.4
File Interpreter ABI Platform
dynamicscope-1.0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 9.2 kB

Release files / dynamicscope-1.0.4.tar.gz

Download URL dynamicscope-1.0.4.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8b5ffd2cb880ed220c44be1323c279669da7dd0adb8f224eb2b575cbad500b3f
BLAKE2b-256 checksum
How to use checksums
2f3eb9ec7111b061110054baa8e40fd2a4b7fb58f02e6b547fda8c7330d88016
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.5

Release files / dynamicscope-1.0.4-py2.py3-none-any.whl

Download URL dynamicscope-1.0.4-py2.py3-none-any.whl
Size 4.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
729ac8ef26557609352a48df8b1a4130dad45cc4a03bff06a16330cf464c8c8f
BLAKE2b-256 checksum
How to use checksums
1131d6cb7091dffc4859fb1a6a754b78af36f4f5066613b1231b1f95f2f958ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.5

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page