0

I have a function that takes a given variable as an argument. This variable is a numpy array. Example in pseudo-code:

def foo(var):
   if 'test' in the name of var:
      do something

return

I have tried different options, like

if 'test' in var: 

or

if var == 'test_complete_name_of_var'

But these just check the values of the array, and not the variable name. I was hoping there could be some sort of trick such as:

if 'test' in var.name()

Any ideas on how to solve this?

jotNewie
  • 426
  • 4
  • 17
  • 4
    Why do you think you need to do this? – Paul M. Aug 03 '20 at 13:52
  • You can't easily do that. Why not change the function signature to `def foo(var, test=False):`? – xjcl Aug 03 '20 at 13:53
  • 1
    Usually if you want to do something like that you can create a dictionary and store the variable under a name. This way you can also check for it. – Andreas Aug 03 '20 at 13:56
  • 1
    related: https://stackoverflow.com/questions/2749796/how-to-get-the-original-variable-name-of-variable-passed-to-a-function – FObersteiner Aug 03 '20 at 13:57
  • 2
    might also be helpful: https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string – FObersteiner Aug 03 '20 at 13:58
  • I think you're missing the way that names work in Python work on many levels. In the function, your name is hard-coded to `var`. You don't need a function to tell you that. Outside the function, many names can refer to the same object. Which one do you want to use? – Mad Physicist Aug 03 '20 at 14:18
  • I think the solution with the dictionary could work for the current problem. The reason to do it: the variables that belong to a certain group of variables should be treated a bit different within the same function that the variables that belong to another group. I thought this could be a very simple way to do it, as the variable names in the code are unique. – jotNewie Aug 03 '20 at 14:48
  • @MadPhysicist I don't want the function to tell me the name of the variable. I want the function to act slightly different according to a substring present in the variable name. I think it's different, or otherwise, I am not understanding your point. – jotNewie Aug 03 '20 at 14:50
  • @jotNewie. You named argument `var`. It is hard coded. So you can do `if 'test' in 'var':`. If that is not what you are looking for, please clarify the question. Either I don't understand what you are trying to do, or you do not understand how names work in python... – Mad Physicist Aug 03 '20 at 14:53
  • @Andreas could you put your answer as a proper answer, so that I can mark it as the solution? Thanks! – jotNewie Aug 03 '20 at 14:54
  • @joNewie yes, thank you for accepting it .! – Andreas Aug 03 '20 at 17:45

2 Answers2

1

Usually if you want to do something like that you can create a dictionary and store the variable under a name. This way you can also check for it.

Example:

dict_variables = {}
dict_variables["var_1"] = ["a", "b", "test"]

def foo(var_name):
    var_value = dict_variables.get(var_name)
    if 'var_' in var_name:
       print(var_value)
       
foo("var_1")
#out: ['a', 'b', 'test']

Usefull e.g. for path and dataframe variables.

Andreas
  • 8,694
  • 3
  • 14
  • 38
0

First, install the varname library

pip install python-varname

then use it as the following example:

from varname import nameof

def foo(var):
    if 'test' in nameof(var):
      ###do something
Hussein Fawzy
  • 366
  • 2
  • 16