Skip to main content

Retrieving variable names of function or class calls.

Project description

python-varname

Pypi Github PythonVers Travis building Codacy Codacy coverage

Retrieving variable names of function or class calls

Installation

pip install python-varname

Usage

Retrieving the variable name inside a function

from varname import varname
def function():
    return varname()

func = function()
# func == 'func'

# available calls to retrieve
func = function(
    # ...
)

func = \
    function()

func = function \
    ()
# calls lead to failure of retrieving
func = [function()]

Function with long argument list

def function(*args):
    return varname()

func = function(
    1, # I
    2, # have
    3, # a
    4, # long
    5, # argument
    6, # list
)

# func == 'var_0'

def function(*args):
    return varname(context = 20)

func = function(
    1, # I
    2, # have
    3, # a
    4, # long
    5, # argument
    6, # list
)

# func == 'func'

varname calls being buried deeply

def function():
    # I know that at which stack this will be called
    return varname(caller = 3)

def function1():
    return function()

def function2():
    return function1()

func = function2()
# func == 'func'

Retrieving instance name of a class object

class Klass:
    def __init__(self):
        self.id = varname()
    def copy(self):
        return varname()

k = Klass()
# k.id == 'k'

k2 = k.copy()
# k2 == 'k2'

varname calls being buried deeply for classes

class Klass:
    def __init__(self):
        self.id = self.some_internal()

    def some_internal(self):
        return varname(caller = 2)

    def copy(self):
        return self.copy_id()

    def copy_id(self):
        return self.copy_id_internal()

    def copy_id_internal(self):
        return varname(caller = 3)

k = Klass()
# k.id == 'k'

k2 = k.copy()
# k2 == 'k2'

In case of failure to retrieve the name

varname has a static index starts from 0 to mark the variable name with failure.

func = [function()]
# func == ['var_0']
func = function \
    ()
# func == 'var_1'

Limitations

  • Calls have to be written in desired format
  • Context have to be estimated in advance, especially for functions with long argument list
  • You have to know at which stack the function/class will be called
  • For performance, since inspection is involved, better cache the name
  • Aliases are not supported
    def function():
      return varname()
    func = function
    
    x = func() # unable to detect
    
  • False positives
    def func(**kwargs):
        return varname()
    x = func(
        y = func()
    )
    # x == 'y'
    
    # to avoid this, you have to write the kwargs
    # in the same line where the is called
    x = func(y=func())
    # x == 'x'
    

Project details


Download files

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

Files for python-varname, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size python_varname-0.0.3-py3-none-any.whl (3.3 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size python-varname-0.0.3.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page