Skip to main content

No project description provided

Project description

ContextManaged Assign

This Python project provides a context manager for temporarily assigning a value to a variable or attribute. The original value is restored when the context is exited.

Usage

from contextmanaged_assign import assign

l = [1, 2, 3]

assert l[0] == 1
with assign("l[0]", 2):
    assert l[0] == 2
assert l[0] == 1

Please note that assign cannot modify local variables themselves. For example, the following code will not work:

x = 1
with assign("x", 2):
    assert x == 2  # AssertionError: 1 != 2
assert x == 1

Installation

Simply install the package using pip:

pip install contextmanaged-assign

Then import the assign function from the contextmanaged-assign module.

License

MIT

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

contextmanaged_assign-0.1.0.tar.gz (2.4 kB view hashes)

Uploaded Source

Built Distribution

contextmanaged_assign-0.1.0-py3-none-any.whl (3.1 kB view hashes)

Uploaded Python 3

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