Skip to main content

Emulate dynamic scope in Python.

Project description

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' was 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 to 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dynamicscope-1.0.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

dynamicscope-1.0.2-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dynamicscope-1.0.2.tar.gz.

File metadata

  • Download URL: dynamicscope-1.0.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.5

File hashes

Hashes for dynamicscope-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e9592d0d4db9c5b55116eb31d44571113fba94da2eb89053535d36c14ceae6c8
MD5 0d14bf802086c66a1a94744c09718d98
BLAKE2b-256 50b9b7d90624d87c518e2647f1b2e730acfebacb7f508e645672e2a5f3fe89c3

See more details on using hashes here.

File details

Details for the file dynamicscope-1.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dynamicscope-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ca39fb64a230f6cf6eaaadb1f59e1e2a39f90504370d2b2edaacc7cb2b235628
MD5 fd715b770f014029b7c5d57e978e0eb6
BLAKE2b-256 7dc83ce0b3ed6ab696f7261ca729e1b060c1c8d165ad84272ed0447e916f8b20

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page