--- id: varname version: "1.0.0" license: MIT license_treatment: permissive maintenance: active --- # varname — Dark magics about variable names in python. License: permissive · Maintenance: active · Downloads: 414.9K/mo ## 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 above — 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 pip install varname uv add varname 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_current - Install friction: low - Maintenance: active - Downloads: 414.9K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags get variable name at runtime, retrieve function argument names, detect variable assignment name, introspect variable names python, runtime variable name inspection, function argument source code, attribute name detection, metaprogramming, introspection, debugging [View on SkillFed](https://skillfed.io/packages/varname) · [View on PyPI](https://pypi.org/project/varname/)