Skip to main content

Probably the best way to simulate block scopes in Python.

Project description

Probably the best way to simulate block scopes in Python.

This is a package, as it says on the tin, to emulate block scoping in Python, the lack of which being a clever design choice yet sometimes a trouble.

In addition to readability and code organization (where your mileage may vary), block scoping in particular helps to have variables garbage collected as soon as possible, which is useful for situations where variables may refer to expensive resources (e.g. GPU arrays).

This package is designed to be as easy to use as possible, with the least mental burden on the user, whilst the implementation being necessarily confusing and cryptic due to the tricks used.

Usage

Other than installing from PyPI the usual way, you can also directly download scoping.py to where your main script is to use the package.

To start a scoped block (where the variables created in the block is to be deleted after the block) use

with scoping():
    ....

Within a block, you can selectively let a variable leak through to the outer scope (as in the traditional behavior in Python) by passing the name (as a string) to

scoping.keep()

If you are only after using the library, just consider scoping and scoping.keep as some kind of quasi-keywords rather than real classes/objects.

See

from scoping import scoping
a = 2
with scoping():
    assert(2 == a)
    a = 3
    b = 4
    scoping.keep('b')
    assert(3 == a)
assert(2 == a)
assert(4 == b)

In the rare case that you would like to reserve the word "scoping" for other uses, the class can be renamed arbitrarily using the following trick:

import scoping
class VeryScoped(scoping.scoping): pass
del scoping

Then the names "VeryScoped" and "VeryScoped.keep" can be used instead, whereas the name "scoping" can be put to other uses at will.

Blocks can be nested, as well as used in functions (unlike prior art), at the price of relying on some CPython specific feature.

Motivation/Prior Art

See https://github.com/bskinn/tempvars with a similar idea but not the intended use for general programming, as well as a more mentally demanding interface (IMHO).

Compatibility

Tested with python 3.6/7/8/9 under Linux

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

scoping-0.1.2.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file scoping-0.1.2.tar.gz.

File metadata

  • Download URL: scoping-0.1.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.8

File hashes

Hashes for scoping-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9bf65830b0e43fc34880842503f551021d980699ee1b74c4d60f0611ac88e607
MD5 171a80dde8fa0e904b4d4fff8b1dd967
BLAKE2b-256 b9e7d0aa1b1c13acc3a0fe58163139204bfc353c156d20ca6f9db1d2baf09c96

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