skillfed

varname

Dark magics about variable names in python.

varname v1.0.0 414.9K downloads/30d#6,828 on PyPI357
Permissive license MIT Active released

What it is and what it does

Varname is a metaprogramming library that uses stack frame inspection to retrieve the names of variables that function or class calls are assigned to, from inside those functions or classes. It exposes this core capability through functions like `varname()` (to get the assignment variable name), `argname()` (to fetch the names or source of arguments passed to a function), and `will()` (to detect the next attribute access). The library depends on executing for bytecode inspection and typing-extensions for type hints.

The package is useful when you need runtime introspection of variable names—for instance, to automatically populate an object's `__varname__` attribute with the name it was assigned to, to create dictionaries without explicitly repeating key names, or to implement debugging utilities that print variable names alongside their values. It handles complex cases like multiple assignment, nested unpacking, and calls wrapped through intermediate functions.

Use it for:

  • Automatically populate an object's id or name field with the variable name it was assigned to.
  • Create dictionaries from variables without repeating key names (jsobj helper).
  • Implement debug utilities that print variable names and values together.
  • Fetch the names or source expressions of arguments passed to a function.
  • Detect which attribute will be accessed next on an object (will function).

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Retrieves variable names at runtime from inside functions and classes, and provides utilities to fetch argument names and detect attribute access patterns.

Yes. The package is actively maintained, has low install friction, carries a permissive MIT license, and solves a real metaprogramming need. It is well-documented with concrete examples. Install it if you need runtime variable name introspection; skip it if your code does not require that capability.

Install

varname on PyPI

pip

pip install varname

uv

uv add varname

poetry

poetry add varname

Installing varname

Before you install

Low install friction with only two runtime dependencies (executing and typing-extensions). Actively maintained with recent commits; last release 120 days ago and repository shows 357 stars.

License in practice

MIT license permits free use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.

Quickstart

from varname import varname

def function():
    return varname()

func = function()  # func == 'func'

Requires Python 3.9 or later; earlier versions need varname < 0.11.

Verify before relying

  • Whether frame inspection works reliably across all Python 3.9+ implementations and edge cases.
  • Performance characteristics when called repeatedly in hot code paths.
  • Compatibility with async/await patterns beyond the asyncio.run example shown.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.9)
Install friction low — pure-Python wheel
Runtime dependencies 2 — executing, typing-extensions
Maintenance actively maintained — 120 days since the last release
Last repo commit
First released
Downloads 414,920/month — #6,828 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: varname-1.0.0-py3-none-any.whl

Tags

get variable name at runtimeretrieve function argument namesdetect variable assignment nameintrospect variable names pythonruntime variable name inspectionfunction argument source codeattribute name detection
metaprogrammingintrospectiondebugging

More Software Development packages