Really small library to implement NamedExpressions in Python 3.8 to older versions of Python.
NamedAssignments is a library that I developed for myself to use named assignments in my Python <= 3.7 projects.
What is named assignments?
Actual thing I don't even know how should I call them but this is the best name I found for them. If you've read PEP 572 there is a new operator coming in Python 3.8 called as Named Expressions (this is how it is going to look like in your code
:=) which helps you to write more clean and shorter code.
For original examples you can check PEP 572 examples directly but if you are looking this library you are probably not using Python 3.8 so just stay in here and continue reading.
Caching computationally expensive computations (This is definitely a bad example of expensive computation but lets just act like it is)
# Makes two addition every time strange_list = [[x + 1, x/(x + 1)] for x in range(5)]
# Makes only one addition every time strange_list = [[N('y', lambda: x + 1), x/N.y] for x in range(5)]
Usage in while loops
# conn is a accepted socket connection while True: data = conn.recv(1024) if not data: break conn.sendall(data)
# conn is a accepted socket connection while N('data', conn.recv, 1024): conn.sendall(N.data)
Usage is not clean as original due to limitations but it is the cleanest way I found.
from namedassignments import NamedAssignments as N N(variable_name, function, *args, **kwargs)
Library is avaible on PyPi so just run
pip install namedassignments
Since the whole code is only 5 lines there is nothing too much. Library uses Python's magic method
__new__ to return what function returns this is all library does on the background.
variable_name: Variable name you want to use while retrieving data. (String) (Required)
function: Function you want to execute. (Function) (Required)
*args: Arguments you want to pass function. (List) (Optional)
**kwargs: Keyword arguments you want to pass function. (Dict) (Optional)
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size & hash||File type||Python version||Upload date|
|namedassignments-1.0.2-py3-none-any.whl (3.5 kB) View hashes||Wheel||py3|
|namedassignments-1.0.2.tar.gz (2.4 kB) View hashes||Source||None|
Hashes for namedassignments-1.0.2-py3-none-any.whl