Utility for local block scoped variables
Project description
Block Local Scope
This simple package allows for variables to be block scoped, which is a common feature of many languages but is missing in Python.
Here is an example of the most basic of uses:
from blockscope import Local
with Local(x=1, y=2) as local:
print( local.x, local.y ) # prints: 1 2
local.z = 3 # declare new variable
print( local.z ) # prints: 3
# when `with` exits: local's x, y, and z
# are cleaned up automatically
Tuple Unpacking
One of the more useful features is tuple (or any iterable) unpacking:
def foo():
return 1, 2, 3
with Local('x, _, z', foo()) as local:
print( local.x, local.z ) # prints: 1 3
Note the placeholder _ to ignore the second element of the tuple. This is in step with Python's regular syntax for tuple unpacking: x, _, z = foo()
You can use *, ?, and ~ modifiers to fine tune the unpacking.
Here are some examples:
def bar():
return 4, 5
with Local('x, _?, y? , z~ ,_*', bar()) as local:
# x local.x is set to the first element of the tuple (4)
# _? second element of the tuple (5) will be ignored if present
# If _ was by itself and element not present an exception
# would be raised: AttributeError with a helpful message
# y? local.y is set to 3rd element of tuple if present,
# In this case there's no 3rd element and local.y will
# not exist.
# z~ local.z would be set if present or set to None otherwise
# In this case local.z will be None
# _* ignore the rest of the tuple elements. Always used
# in the last position with a placeholder _ or by itself
print( local.x, local.z) # prints: 4 None
You can chain multiple un-packings and declarations:
with Local('x,y,*', foo(), '_,z' = bar(), i=6, j=7) as local:
print( local.x, local.y, local.z ) # prints: 1 2 5
print(i, j) # prints: 6 7
Dictionary and List Unpacking
Dictionaries {str:any} and lists of tuples [(str, any)] are unpacked automatically if not assigned to a keyword argument:
d = {'x':1, 'y':2, 'z':3}
l = [('a': 4), ('b': 5), ('c': 6)]
with Local(d, l) as local:
print( local.x, local.y, local.z ) # prints: 1 2 3
print( local.a, local.b, local.c ) # prints: 4 5 6
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file blockscope-0.1.tar.gz.
File metadata
- Download URL: blockscope-0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ac116f9b33f48c04af3e2d756626bd7dddb5f1af0e41642b5d6559128e732d0
|
|
| MD5 |
73e21cd89ac75f0dcc39f4356c479c66
|
|
| BLAKE2b-256 |
e7a1bc9cb889c355865ab91880f6a139dd1367bd07c12c052e87b90cccce8405
|
File details
Details for the file blockscope-0.1-py3-none-any.whl.
File metadata
- Download URL: blockscope-0.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1d1ed500b96f34dbf1523cd4033552569e89911ae48c23ab9e387f81681aae2
|
|
| MD5 |
311b3c40c608749277383f1e17fb1ef7
|
|
| BLAKE2b-256 |
fad6d17af4bd003248920a22ee55ad27e8a74708281ff2e1f0e21b261d02f2d3
|