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.2.0.tar.gz (3.5 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.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_nameof-1.2.0.tar.gz
  • Upload date:
  • Size: 3.5 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.0.tar.gz
Algorithm Hash digest
SHA256 e4f69657327d5fbfa0563e8da41a441d433e5ef6960ed29dfb1e8fda975e1646
MD5 192de51c1d54a525c2167296688d3f16
BLAKE2b-256 9cc5def8743411ae56eba41993d57cfd9e52342aadebea2f5d897fa79baa2527

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_nameof-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0386fff867a7438bfb68479952c55d320b856a0957c91568dc746d2aab2d061
MD5 855dcb7cda402fb9efd7ab0ff7ac287f
BLAKE2b-256 45a5416aa0850f9e32e53ca3196950df1e21306da89de4e2b5ace83f4e90e6ef

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