Attaching and detaching namespaces to keep globals clean
Project description
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
inattach()
. -
attach()
only concerns itself with globals. If you call it inside a function, beware unexpected behaviour.
To Do
- Add a
read_only
paramater toattach()
, 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file attach-0.1.tar.gz
.
File metadata
- Download URL: attach-0.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18f2e86a7263406d3d97ed78db9f5c1cc7a07f9b1a488e76fe199d984f8b49b4 |
|
MD5 | c891884141f134066f547458ad4f2bec |
|
BLAKE2b-256 | 697b27795c87fe7c1d0380cdb8149c3526fc9c17a764e89689437e674da4fd00 |
File details
Details for the file attach-0.1-py3-none-any.whl
.
File metadata
- Download URL: attach-0.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 355d801bc94c778abf45e26eee11e79791db887461fd008b5dbe8ced92b98d60 |
|
MD5 | 650102d8c5502674e28cfcfdf60bbba7 |
|
BLAKE2b-256 | 7dc74cf3b585f6b07e6c801546b48ad0b7fa1063c5eaffa0654b63ba4829cb9f |