Is there a better way to write this?

Is there a better way to do this? I feel like I'm doing something wrong, repeating myself too much.

O = viz.pick(1, viz.WORLD) BackSetts = ["set_b1b", "set_b2a", "set_b1a", "set_b2b"] LeftSetts = ["set_l1a", "set_l1b", "set_l2a", "set_l1b"] NormSetts = ["set_nr_a", "set_nr_b"] Maps = ["MapA","MapB"] if O.name in BackSetts: for i in set(BackSetts)|set(Maps): WORLD[i].alpha(abs(WORLD[i].getAlpha()-1)) elif O.name in LeftSetts: for i in set(LeftSetts)|set(Maps): WORLD[i].alpha(abs(WORLD[i].getAlpha()-1)) elif O.name in NormSetts: for i in NormSetts: WORLD[i].alpha(abs(WORLD[i].getAlpha()-1)) 
+6
python
source share
1 answer

Trivial Transformation:

 O = viz.pick(1, viz.WORLD) BackSetts = ["set_b1b", "set_b2a", "set_b1a", "set_b2b"] LeftSetts = ["set_l1a", "set_l1b", "set_l2a", "set_l1b"] NormSetts = ["set_nr_a", "set_nr_b"] Maps = ["MapA","MapB"] anyset = [] if O.name in BackSetts: anyset = set(BackSetts)|set(Maps) elif O.name in LeftSetts: anyset = set(LeftSetts)|set(Maps) elif O.name in NormSetts: anyset = NormSetts for i in anyset: WORLD[i].alpha(abs(WORLD[i].getAlpha()-1)) 

This will ensure that NormSetts does not integrate with Maps, as in your source code.

+3
source share

All Articles