Skip to main content

attach

You can install this Python package using pip install attach.

How does this work?

Here is an example session:

>>> from attach import Namespace, attach
>>>
>>> foo = Namespace()
>>> with attach(foo):
...    bar = 'bar'
...    baz = 'baz'
...
>>>
>>> bar
Traceback (most recent call last):
    ...
NameError: name 'bar' is not defined

Notice that since we defined bar inside the attach(foo) context, bar does not exist as a global. Instead, it's value is saved to the namespace foo:

>>> foo.bar
'bar'

We can reattach the namespace later:

>>> with attach(foo):
...     print(bar)
...
bar

What’s the point?

The module is especially useful inside Jupyter notebooks. Quite often, we have constructions like this:

X = np.array([1, 2, 3])
def increment(X):
    return X + 1

In other words, we have globals and function parameters or local variables by the same name. However, this can lead to unintended references in the case of a typo, or difficulty keeping everything modular.

By keeping variables out of globals and inside namespaces, you can force functions to only use the variables that have been explicitly passed in, thereby preventing many tricky bugs!

Isn’t hacking globals evil?

Yes, for good reason, but this is not meant to be used in code files or in production, bur rather in exploratory Jupyter notebooks.

What’s this Namespace class?

You can pass any dictionary-like object into attach(); it does not have an instance of the Namespace class defined in this package.

The Namespace class just has a few niceties, like a nice string representation, and allowing you to attributes both as foo.bar and foo['bar'].

Stuff you should know

  • By default, variables beginning with an underscore are not saved to the namespace; they are lost. To change this behaviour, set skip_underscored=False in attach().

  • attach() only concerns itself with globals. If you call it inside a function, beware unexpected behaviour.

To Do

  • Add a read_only paramater to attach(), which enables using namespaces in a nested way when neeed.
  • Check if being called inside a function, and error out unless read_only=True since we can’t modify function locals, only globals.

Release files for attach 0.1

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

Source distribution (sdist)

Source distribution for attach 0.1
File Size Uploaded
attach-0.1.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for attach 0.1
File Interpreter ABI Platform
attach-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.0 kB

Release files / attach-0.1.tar.gz

Download URL attach-0.1.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
18f2e86a7263406d3d97ed78db9f5c1cc7a07f9b1a488e76fe199d984f8b49b4
BLAKE2b-256 checksum
How to use checksums
697b27795c87fe7c1d0380cdb8149c3526fc9c17a764e89689437e674da4fd00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.6

Release files / attach-0.1-py3-none-any.whl

Download URL attach-0.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
355d801bc94c778abf45e26eee11e79791db887461fd008b5dbe8ced92b98d60
BLAKE2b-256 checksum
How to use checksums
7dc74cf3b585f6b07e6c801546b48ad0b7fa1063c5eaffa0654b63ba4829cb9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.6

Release history Release notifications | RSS feed

This release

0.1 This release

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