Skip to main content

Match-Ref

Top-Level Variable References For Python's match

zero dependencies PyPI - Python Version PyPI


Objective

This library provides a convenient interface to variables in the local and global namespaces, using qualified (dotted) names.

Python 3.10 introduced the match-case structure which allows you to write flow control code like this:

my_choice = input("Your choice: ")
fruit_on_sale = "peach"

match my_choice:
    case "apple": print("Delivering an apple.")
    case "banana": print("Delivering a banana.")
    case fruit_on_sale:
        print("Good choice!")

If you now entered a value like tomato, he output would be fruit_on_sale. This is, because fruit_on_sale is considered a placeholder/capture pattern and not a reference to a variable.

Therefore, we need another way to reference variables. We can accomplish this using dotted names (i.e. fruit_data.fruit_on_sale, because Python does not interpret these names as placeholders.

Installation

You can install this package using pip :

  • pip install match-ref on all platforms, if available
  • python3 -m pip install match-ref on Unix
  • py -m pip install match-ref on Windows

Usage

from matchref import ref

my_choice = "banana"
fruit_on_sale = "peach"
match my_choice:
    case ref.fruit_on_sale: print("Good choice!")

You can use the ref helper to resolve all variables in the current local and global namespaces which you could usually reference without using a dotted name.

You can use ref.any_local_or_global_variable_name in any expression, not only inside case conditions, if you need to.

Only value retrieval is supported, as attribute-setting is not the purpose of this library.

How does it work?

ref is the default instance of the ScopeReference class, defined in match-ref. It has a method __getattr__ which gets called whenever you request an attribute of ref. This method then starts looking up the local namespace it was called from (your script or your function) and, if it doesn't find the variable you're looking for, continues searching the global namespace as well.

If it doesn't find the variable you are looking for, the following error is being raised:

ValueError: referenced name '...' is not defined

Metadata

Release files for match-ref 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for match-ref 1.0.1
File Size Uploaded
match-ref-1.0.1.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for match-ref 1.0.1
File Interpreter ABI Platform
match_ref-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.2 kB

Release files / match-ref-1.0.1.tar.gz

Download URL match-ref-1.0.1.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
71e06d8096c12e184651353c150174c73f3bedc773fc67133cb17673f12e337e
BLAKE2b-256 checksum
How to use checksums
bff54a9faf6e92c6666df6a38f17a08d659ec8be5254415a79b1b0deefdffac3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.6

Release files / match_ref-1.0.1-py3-none-any.whl

Download URL match_ref-1.0.1-py3-none-any.whl
Size 3.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9d918d3f85a6cf32e392f6fb7b5324de2943f7099f7446fd9e7c70c0fd85feba
BLAKE2b-256 checksum
How to use checksums
8c14c9ed1866b650231eec99060ce9f54bc17a0b0cb4f70f98c099d7c5641df3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.6

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page