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 nameofVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use the `nameof` function to get the string representation of variable and attribute names.
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.
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.
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.
Ensure the package is installed (`pip install nameof`) and the function is imported correctly at the top of your file: `from nameof import nameof`.
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.