Question on parameters and python. get/setattr vs. direct access

I have noticed that a lot of the Widgets use setattr and getattr to set and get parameters from ops. Is there a difference between this method and access methods like op(‘something’).par.Value ? Is it safer or faster or?

Or there is no real difference, it’s just a stylistic thing?

Thank you.


The reason to use getattr or setattr is, it enables you to use variables for (part of ) the parameter names.
For instance if you need to get the value of a custom parameter which name is either Maxscorelevel1 or Maxscorelevel2 or Maxscorelevel3 depending on the corresponding variable currentLevel ( 1 or 2 or 3) which you already have as an integer, you can get the correct parameter value for the current level of your game by doing:
getattr(op('mycomp').par, "Maxscorelevel" + str(currentLevel))

In the cases where you don’t need the extra functionality nettoyeur mentioned, definitely avoid the getattr() as we can optimize the regular syntax better, and it’ll likely go through the Optimized Expression pathway instead.

Ahh ok. Makes sense. Good to know :smiley:

Thank you, Nettoyeur. Very helpful.