{"id":8340,"library":"nameof","title":"nameof","description":"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.\"","status":"abandoned","version":"0.0.1","language":"en","source_language":"en","source_url":"https://github.com/alexmojaki/nameof","tags":["variable names","reflection","metaprogramming","developer tool"],"install":[{"cmd":"pip install nameof","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Required runtime environment.","package":"Python","optional":false}],"imports":[{"symbol":"nameof","correct":"from nameof import nameof"}],"quickstart":{"code":"from nameof import nameof\n\ndef example_function():\n    my_variable = 123\n    another_name = 'hello'\n    \n    print(f\"The name of 'my_variable' is: {nameof(my_variable)}\")\n    print(f\"The name of 'another_name' is: {nameof(another_name)}\")\n\n    class MyClass:\n        def __init__(self, value):\n            self.value = value\n    \n    instance = MyClass(42)\n    print(f\"The name of 'instance.value' is: {nameof(instance.value)}\")\n\nexample_function()","lang":"python","description":"This quickstart demonstrates how to use the `nameof` function to get the string representation of variable and attribute names."},"warnings":[{"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.","message":"The 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.","severity":"breaking","affected_versions":"0.0.1"},{"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.","message":"The `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).","severity":"gotcha","affected_versions":"0.0.1"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"0.0.1"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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`.","cause":"The `nameof` function was called without being properly imported or the `nameof` package was not installed.","error":"NameError: name 'nameof' is not defined"},{"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.","cause":"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.","error":"AttributeError: 'SomeType' object has no attribute 'some_attribute' (when using nameof on an attribute that exists)"}]}