Home

Awesome

varname

Pypi Github PythonVers Building Docs and API Codacy Codacy coverage Downloads

Dark magics about variable names in python

CHANGELOG | API | Playground | :fire: StackOverflow answer

Installation

pip install -U varname

Note if you use python < 3.8, install varname < 0.11

Features

Credits

Thanks goes to these awesome people/projects:

<table> <tr> <td align="center" style="min-width: 75px"> <a href="https://github.com/alexmojaki/executing"> <img src="https://ui-avatars.com/api/?color=3333ff&background=ffffff&bold=true&name=e&size=400" width="50px;" alt=""/> <br /><sub><b>executing</b></sub> </a> </td> <td align="center" style="min-width: 75px"> <a href="https://github.com/alexmojaki"> <img src="https://avatars0.githubusercontent.com/u/3627481?s=400&v=4" width="50px;" alt=""/> <br /><sub><b>@alexmojaki</b></sub> </a> </td> <td align="center" style="min-width: 75px"> <a href="https://github.com/breuleux"> <img src="https://avatars.githubusercontent.com/u/599820?s=400&v=4" width="50px;" alt=""/> <br /><sub><b>@breuleux</b></sub> </a> </td> <td align="center" style="min-width: 75px"> <a href="https://github.com/ElCuboNegro"> <img src="https://avatars.githubusercontent.com/u/5524219?s=400&v=4" width="50px;" alt=""/> <br /><sub><b>@ElCuboNegro</b></sub> </a> </td> <td align="center" style="min-width: 75px"> <a href="https://github.com/thewchan"> <img src="https://avatars.githubusercontent.com/u/49702524?s=400&v=4" width="50px;" alt=""/> <br /><sub><b>@thewchan</b></sub> </a> </td> <td align="center" style="min-width: 75px"> <a href="https://github.com/LawsOfSympathy"> <img src="https://avatars.githubusercontent.com/u/96355982?s=400&v=4" width="50px;" alt=""/> <br /><sub><b>@LawsOfSympathy</b></sub> </a> </td> <td align="center" style="min-width: 75px"> <a href="https://github.com/elliotgunton"> <img src="https://avatars.githubusercontent.com/u/17798778?s=400&v=4" width="50px;" alt=""/> <br /><sub><b>@elliotgunton</b></sub> </a> </td> </tr> </table>

Special thanks to @HanyuuLu to give up the name varname in pypi for this project.

Usage

Retrieving the variable names using varname(...)

The decorator way to register __varname__ to functions/classes

Getting variable names directly using nameof

from varname import varname, nameof

a = 1
nameof(a) # 'a'

b = 2
nameof(a, b) # ('a', 'b')

def func():
    return varname() + '_suffix'

f = func() # f == 'f_suffix'
nameof(f)  # 'f'

# get full names of (chained) attribute calls
func.a = func
nameof(func.a, vars_only=False) # 'func.a'

func.a.b = 1
nameof(func.a.b, vars_only=False) # 'func.a.b'

Detecting next immediate attribute name

from varname import will
class AwesomeClass:
    def __init__(self):
        self.will = None

    def permit(self):
        self.will = will(raise_exc=False)
        if self.will == 'do':
            # let self handle do
            return self
        raise AttributeError('Should do something with AwesomeClass object')

    def do(self):
        if self.will != 'do':
            raise AttributeError("You don't have permission to do")
        return 'I am doing!'

awesome = AwesomeClass()
awesome.do() # AttributeError: You don't have permission to do
awesome.permit() # AttributeError: Should do something with AwesomeClass object
awesome.permit().do() == 'I am doing!'

Fetching argument names/sources using argname

from varname import argname

def func(a, b=1):
    print(argname('a'))

x = y = z = 2
func(x) # prints: x


def func2(a, b=1):
    print(argname('a', 'b'))
func2(y, b=x) # prints: ('y', 'x')


# allow expressions
def func3(a, b=1):
    print(argname('a', 'b', vars_only=False))
func3(x+y, y+x) # prints: ('x+y', 'y+x')


# positional and keyword arguments
def func4(*args, **kwargs):
    print(argname('args[1]', 'kwargs[c]'))
func4(y, x, c=z) # prints: ('x', 'z')


# As of 0.9.0 (see: https://pwwang.github.io/python-varname/CHANGELOG/#v090)
# Can also fetch the source of the argument for
# __getattr__/__getitem__/__setattr/__setitem__/__add__/__lt__, etc.
class Foo:
    def __setattr__(self, name, value):
        print(argname("name", "value", func=self.__setattr__))

Foo().a = 1 # prints: ("'a'", '1')

Value wrapper

from varname.helpers import Wrapper

foo = Wrapper(True)
# foo.name == 'foo'
# foo.value == True
bar = Wrapper(False)
# bar.name == 'bar'
# bar.value == False

def values_to_dict(*args):
    return {val.name: val.value for val in args}

mydict = values_to_dict(foo, bar)
# {'foo': True, 'bar': False}

Creating dictionary using jsobj

from varname.helpers import jsobj

a = 1
b = 2
jsobj(a, b) # {'a': 1, 'b': 2}
jsobj(a, b, c=3) # {'a': 1, 'b': 2, 'c': 3}

Debugging with debug

from varname.helpers import debug

a = 'value'
b = ['val']
debug(a)
# "DEBUG: a='value'\n"
debug(b)
# "DEBUG: b=['val']\n"
debug(a, b)
# "DEBUG: a='value'\nDEBUG: b=['val']\n"
debug(a, b, merge=True)
# "DEBUG: a='value', b=['val']\n"
debug(a, repr=False, prefix='')
# 'a=value\n'
# also debug an expression
debug(a+a)
# "DEBUG: a+a='valuevalue'\n"
# If you want to disable it:
debug(a+a, vars_only=True) # ImproperUseError

Replacing exec with exec_code

from varname import argname
from varname.helpers import exec_code

class Obj:
    def __init__(self):
        self.argnames = []

    def receive(self, arg):
        self.argnames.append(argname('arg', func=self.receive))

obj = Obj()
# exec('obj.receive(1)')  # Error
exec_code('obj.receive(1)')
exec_code('obj.receive(2)')
obj.argnames # ['1', '2']

Reliability and limitations

varname is all depending on executing package to look for the node. The node executing detects is ensured to be the correct one (see this).

It partially works with environments where other AST magics apply, including pytest, ipython, macropy, birdseye, reticulate with R, etc. Neither executing nor varname is 100% working with those environments. Use it at your own risk.

For example: