nameof

0.0.1 · abandoned · verified Thu Apr 16

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."

Common errors

Warnings

Install

Imports

Quickstart

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()

view raw JSON →