Skip to main content

A Python implementation of the C# nameof operator.

Project description

python-nameof: a Python implementation of the useful C# nameof operator

A Python utility that mimics the C# nameof operator, allowing you to get variable, attribute, or property names as strings at runtime.

It is an essential operator to allow flexible and reliable refactoring, among many other things.

This implementation supports string interpolation, so it's easy to reference variable names when logging, e.g.
log.error(f"{nameof(somevariable)} is incorrect").

Installation

Pip:

pip install python-nameof

My recommendation is to always use uv instead of pip – I personally think it's the best package and environment manager for Python.

uv add python-nameof

Usage

Import:

from nameof import nameof

Simple usage:

foo = 123
print(nameof(foo))  # Output: 'foo'

It supports string interpolation, so it's easier to reference variable names when logging, allowing for easier refactoring.
In the example below, refactoring the name of second_param will propagate to the printed message without having to manually do it.

def myFuncWithAmazingLogging(first_param: int, second_param: int):
    valid_threshold = 10
    if second_param < 10:
        print(f"The parameter {nameof(second_param)} should be less than {valid_threshold}")

It works for class attributes and instance variables.

class Bar:
    attr = 99
bar = Bar()
print(nameof(Bar.attr))      # Output: 'attr'
print(nameof(bar.attr))      # Output: 'attr'

class MyClass:
    def __init__(self):
        self.instance_var = 123

obj = MyClass()
print(nameof(obj.instance_var))  # Output: 'instance_var'

class Inner:
    @property
    def value(self):
        return 10

class Outer:
    def __init__(self):
        self.inner = Inner()

outer = Outer()
print(nameof(outer.inner.value))  # Output: 'value'

Error Handling

If you pass a value or an expression that is not a variable or attribute, nameof raises a ValueError:

nameof(42)            # Raises ValueError
nameof("foo.bar")     # Raises ValueError
nameof("nameof(bar)") # Raises ValueError

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_nameof-1.2.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_nameof-1.2.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file python_nameof-1.2.1.tar.gz.

File metadata

  • Download URL: python_nameof-1.2.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for python_nameof-1.2.1.tar.gz
Algorithm Hash digest
SHA256 1cbafffc19191ffdbacc41069cf14ca9b1cf1ab26441379229bfacd504baaa05
MD5 491f4ed5d31649983e20c717f9c250e0
BLAKE2b-256 3839501fcafb3fe5712c836c934db155cc11c54b0d6f4a7e4028fb69f46aea28

See more details on using hashes here.

File details

Details for the file python_nameof-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: python_nameof-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for python_nameof-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37f08b0ba271a441baaf71827499be944e53ae8a7db6870bdb901a1499181992
MD5 87ff436184384dbf70150c87234d6cd4
BLAKE2b-256 453145343c85d9c11e3ab2f3a397c7842a4e245962262bd13dfe1ec3f70333de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page