Skip to main content

Really small library to implement NamedExpressions in Python 3.8 to older versions of Python.

Project description


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.

But how?

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)

    Old way:
    # 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

    Old way:
    # conn is a accepted socket connection
    while True:
        data = conn.recv(1024)
        if not data:
    # conn is a accepted socket connection
    while N('data', conn.recv, 1024):


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.

  • __new__ Arguments:
    • 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)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for namedassignments, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size namedassignments-1.0.2-py3-none-any.whl (3.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size namedassignments-1.0.2.tar.gz (2.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page