Attaching and detaching namespaces to keep globals clean
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
bar does not exist as a global.
Instead, it's value is saved to the namespace
>>> 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.
You can pass any dictionary-like object into
attach(); it does not have an instance
Namespace class defined in this package.
Namespace class just has a few niceties, like a nice string representation, and
allowing you to attributes both as
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
attach()only concerns itself with globals. If you call it inside a function, beware unexpected behaviour.
- Add a
attach(), which enables using namespaces in a nested way when neeed.
- Check if being called inside a function, and error out unless
read_only=Truesince we can’t modify function locals, only globals.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size & hash SHA256 hash help||File type||Python version||Upload date|
|attach-0.1-py3-none-any.whl (4.1 kB) Copy SHA256 hash SHA256||Wheel||py3||Aug 11, 2018|
|attach-0.1.tar.gz (3.9 kB) Copy SHA256 hash SHA256||Source||None||Aug 11, 2018|