Registry / serialization / nameof

nameof

JSON →
library0.0.1pypypi✓ verified 84d ago

The `nameof` library, version 0.0.1, provides a simple Python function to retrieve the string name of a variable or attribute, similar to C#'s `nameof` operator. It was released in February 2020 and has not seen further updates. Its author explicitly recommends using the more actively maintained `varname` library for this functionality, indicating that `nameof` was a "toy project."

pip install nameof
INSTALL
IMPORT
SIG · NAMEOF
N
nameof
serializationpythonv0.0.1
Install
2.4s avg
Import
23ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.025s · 19.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.4s · import 0.022s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

nameof
from nameof import nameof

This quickstart demonstrates how to use the `nameof` function to get the string representation of variable and attribute names.

from nameof import nameof def example_function(): my_variable = 123 another_name = 'hello' print(f"The name of 'my_variable' is: {nameof(my_variable)}") print(f"The name of 'another_name' is: {nameof(another_name)}") class MyClass: def __init__(self, value): self.value = value instance = MyClass(42) print(f"The name of 'instance.value' is: {nameof(instance.value)}") example_function()
Debug
Known issues
breakingThe author of the `nameof` library explicitly recommends using the `varname` library instead. `nameof` is considered a "toy project" and is not actively maintained, making `varname` the preferred and more robust solution for this functionality.
fix
Migrate to `varname` library: `pip install varname` and use `from varname import nameof` or other `varname` functionalities. The `varname` library provides a `nameof` function with similar capabilities.
affects: 0.0.1
gotchaThe `nameof` library (version 0.0.1) relies on `sys._getframe` for its functionality, which is a CPython-specific implementation detail. This can lead to unexpected behavior or outright failure on other Python implementations (e.g., PyPy, Jython, IronPython).
fix
Be aware of potential incompatibility outside of CPython. If cross-implementation compatibility is critical, consider alternatives or stick to literal strings. The `varname` library, while also using frame inspection, is more actively maintained and may offer better compatibility or clearer limitations.
affects: 0.0.1
gotchaThe `nameof` function has limitations with complex expressions, multiple calls on a single line, or nested parentheses in strings. It might return an incorrect or truncated name in such scenarios.
fix
Avoid using `nameof` with complex expressions directly. Pass simple variable or attribute references to `nameof`. For example, instead of `nameof(my_list[0])`, use `item = my_list[0]; nameof(item)` or a literal string.
affects: 0.0.1
Errors
Common errors & fixes
NameError: name 'nameof' is not defined
The `nameof` function was called without being properly imported or the `nameof` package was not installed.
fix
Ensure the package is installed (`pip install nameof`) and the function is imported correctly at the top of your file: `from nameof import nameof`.
AttributeError: 'SomeType' object has no attribute 'some_attribute' (when using nameof on an attribute that exists)
This error likely indicates `nameof` is failing to correctly parse the expression or access the frame information due to its underlying implementation limitations, especially with certain object types or dynamic contexts. It's a manifestation of its 'gotcha' warnings regarding reliability.
fix
Simplify the expression passed to `nameof`. Ensure the attribute access is direct and not part of a more complex chain. If the issue persists, consider using a literal string for the name or switching to the `varname` library which may handle more edge cases.
Upgrade
Version history
0.0.1latest on PyPI · released Feb 1, 2020
Audit
Dependencies
PythonrequiredRequired runtime environment.
Agent activity
9 hits · last 30 days
node
9
Resources
nameof — pip install nameof · libregistry