0

I want to apply a function to many different objects, without grouping these objects into another object (like a list or a dictionary). For example, my code could look like this:

termxx = switch_values(termxx)
termyy = switch_values(termyy)
state = switch_values(state)
ori = switch_values(ori)
p1n = switch_values(p1n)
p2n = switch_values(p2n)
p3n = switch_values(p3n)

As you see, the structure is very simple, and I wonder whether I could just write that without repeatig functionname() all the time. This is probably a general programming question and has some specific term to it that I don't know. I would appreciate links to / explanations of that concept; specific solutions are welcome for Python.

Lukas
  • 424
  • 3
  • 6
  • 17
  • 2
    The general solution is to put your objects in some container then iterate and apply the function to the elements of that container – juanpa.arrivillaga May 13 '20 at 07:47
  • Thank @juanpa.arrivillaga - I was hoping I would not have to take the "detour" of the container and that there might be a "less cumbersome" method. That clarification is really what I was looking for. – Lukas May 14 '20 at 10:09

3 Answers3

0

Yes, this is a very common programming problem. If you do something repeatedly, you should make a loop. However, an important task is finding how to feed the loop. In your case, you could simply make a list of the variables to process:

vars = [termxx, termyy, state, ori, p1n, p2n, p3n]
for item in vars:
    item = switch_values(item)

To split the list back into different variables, you can use the following line:

termxx, termyy, state, ori, p1n, p2n, p3n = vars
Melebius
  • 6,183
  • 4
  • 39
  • 52
  • Oh yeah, I kind of forgot about the first line - I adapted my original question, since that is indeed out of scope. I guess I have to "unpack" all the objects afterwards if I want to address them, so I had to use `vars[0]` instead of `termxx`. Is there any way to 'unpack' the list into single objects in the global environment? – Lukas May 14 '20 at 10:13
  • 1
    @Lukas You can simply assign the list to separate variables: `termxx, termyy, state, ori, p1n, p2n, p3n = vars`. See [Split list into different variables](https://stackoverflow.com/questions/21791482/split-list-into-different-variables) for details. – Melebius May 14 '20 at 11:48
0
items  = [imc,termxx,termyy,state,ori,p1n,p2n,p3n]

items = [switch_values (item) for item in items]

This is probably the most elegant solution

AlephNot
  • 138
  • 7
0

Dont know if theres a name of the concept but you can make a list and iterate over it.

objects = [imc,termxx,termyy,state,ori,p1n,p2n,p3n]

objects = [switch_values(value) for value in objects]
Shahir Ansari
  • 1,682
  • 15
  • 21
  • Thanks. As stated above, I was kind of looking for a solution without lists because of the unpacking that would be necessary afterwards - specifically, I want to address the objects as `imc` and so on, not using `objects[0]`. – Lukas May 14 '20 at 10:10