Skip to main content

A Python implementation of the C# nameof operator.

Project description

Python implementation of the C# nameof operator

python-nameof

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

It supports string interpolation, so it's easier to reference variable names when logging, allowing for easier and more robust refactoring.

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.1.2.tar.gz (3.7 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.1.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_nameof-1.1.2.tar.gz
  • Upload date:
  • Size: 3.7 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.1.2.tar.gz
Algorithm Hash digest
SHA256 f56c12250319e0b5d93d687f75fabdb49f5a39c16cea3f8b1b8f173e04d876ab
MD5 99e82cd6aef0f2fb7992e44dc3046dca
BLAKE2b-256 840c4e3d67f2e6616eab06259825fff42085292cc8bebb1cbb452a390d61dfab

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_nameof-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.8 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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c39640b951211c8a53006b40648272654a6d05bfbb517df1a50fd01597108084
MD5 eed08a53d801dfb685abbafd75c40a4d
BLAKE2b-256 28ee60b2fef57b2df9cc15c48138247057b15349c7c73393bf010b4b8e4676bb

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