A Python implementation of the C# nameof operator.
Project description
python-nameof: a Python implementation of C# nameof operator
This is a follow-up to this project since the author archived it, preferring the
varnamelibrary which used to covernameof, but thenvarnamedeprecated it.
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):
valid_threshold = 10
if first_param < 10:
print(f"The parameter {nameof(first_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'
It works also for nested classes.
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'
Multiple assignments
If a variable is assigned twice, only the first name is returned.
a = b = 1
nameof(a) # returns "a"
nameof(b) # returns "b"
nameof(1) # raises ValueError (see next section below)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_nameof-1.2.2.tar.gz.
File metadata
- Download URL: python_nameof-1.2.2.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
772e206aa74a8f7fbe8bd08c04542982bff46b18a8b653ca115df74007f5d780
|
|
| MD5 |
afc81fc2c3246f6f2756bbad9d540b3c
|
|
| BLAKE2b-256 |
dd61af359c18daa0fb908060830d0871dc1b94a7801fe668ad2a9723d9790607
|
File details
Details for the file python_nameof-1.2.2-py3-none-any.whl.
File metadata
- Download URL: python_nameof-1.2.2-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56531e5295df28a6e7102044a97b51e4d316980724b1f6612767b9e0d520ad8e
|
|
| MD5 |
4ab847daf45213bd39fd17f37600060a
|
|
| BLAKE2b-256 |
a0f4b7a75c5ce72191c36d9d73714ec287e8a257522757159bfe28ee7d1096cf
|