I don't know what to call this but I've done it in TCL and Elixir. It's when you substitute a variable to make the code simpler. That's a terrible way of explaining it. The example will make it clear:
Here's regular code:
def get(self, item):
if item == 'name':
return self.name
elif item == 'age':
return self.age
I'd like to turn that into something like this:
def get(self, item):
return self.%{item}
Where it interprets the value in the variable item
as the name of a variable. like I said, I've done stuff like this in other languages but I don't know what it's called and I don't know how to do it in python.
Can you help me? What is this ability/style called? Also, how do you do it in Python?
PS. Here's a trivial example from TCL
c:\repos\flow>tclsh
% set foo bar
bar
% set bar baz
baz
% puts $foo
bar
% puts [set $foo]
baz
See how [set $foo]
essentially told the interpreter to interpret $foo
's value as a variable name which was the variable bar
then the command puts
took bar
as a variable and printed out its value which was the string baz
You can even do this kind of thing with commands in Tcl
% set a puts
puts
% $a hello\ world
hello world