Can I use the value of a variable as a parameter name for a function?

George Adams

There are a number of StackOverflow questions asking about using a variable as a variable (like this and this, but I don't think this is a duplicate of those. I want to know if I can use the value of a variable as a function parameter.

For example, let's say I want to call a function that will populate one of two different MySQL tables:

  • The name table's columns are id and fullname.
  • The phone table's columns are id and phonenum
data = get_data()                 # data = a full name or a phone number
data_type = get_data_type(data)   # data_type = "name" or "phone"
....
column_names_for_tables = {'name': 'fullname', 'phone': 'phonenum'}
column_name = column_names_for_tables[data_type]
# column_name now = "fullname" or "phonenum", depending on the value of #data

new_entry = MakeNewEntry(date=datetime.datetime.now(), id=123, <column_name>=data)

Of course that last line is not valid Python, but my goal is to dynamically generate the function call:

new_entry = MakeNewEntry(date=datetime.datetime.now(), id=123, fullname=data)

or

new_entry = MakeNewEntry(date=datetime.datetime.now(), id=123, phonenum=data)

That is, I want to use the value of column_name as the name of my parameter.

Can I do that?

(For this example, please assume that MakeNewEntry() is not under my control - it will accept date, id, and a phonenum or fullname parameter, but can't be refactored.)

Anonymous

You can use the double star operator to pass in arbitrary arguments to a function:

col = 'foo'
val = 'bar'
kwargs = {col: bar}

some_function(**kwargs)

And you can make that shorter by inlining the dictionary like so:

some_function(**{col: var})

You can mix and match the syntax and end up with something like this:

MakeNewEntry(date=datetime.datetime.now(), id=123, **{column_name: data})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

How come I can create a variable with same name as a function parameter?

How can I use a variable's value in a variable name in JavaScript?

Can I use a function parameter to create a global variable?

Use a variable value in a function name

How can I get Matlab to use the variable value instead of name

can i use a variable's value as the name of an object in C#

Why I use list as a parameter of a function and the function can't change the value of the actual parameter but only the formal parameter?

Can I use variable with value from some function

How can I use the value of a variable as a keyword in a function definition?

Can I use a variable as variable name in JavaScript?

How can I use a variable's value to look up another variable whose name is said value

Why can I omit the parameter name to a function?

How can I use an arbitrary name for the function and pass it as a parameter to a member function?

Can I use a query parameter in a table name?

Can I use function parameter in this.state path as wildcard? EX. this.state.{parameter}.value

Can I use templates to create a function that creates a variable without it appearing in function parameter list?

how can i pass variable in parameter as a function?

How can I use the expanded value of a shell variable in the name of another variable?

How can I use variable value as name for another variable (batch file)?

Can I Printf the name of a function assigned to a variable?

How can I use a variable for a key name?

How can i use $_POST if the name is a variable?

Can I use an IF statement as a parameter in a COUNTIF function?

Can I use a void* as a parameter to a function?

Weird examples in javascript, why I can't reassign a global variable's value through a function's parameter?

Why can I not use a variable as parameter in the require() function of node.js (browserify)?

Variable with same name inside function changes the value of the passed parameter

Cannot use let to define a variable with the same name of function's parameter