Home > Mobile >  Loop through instance attributes and set a value to them (not setattr()) [closed]
Loop through instance attributes and set a value to them (not setattr()) [closed]

Time:09-22

I'm trying to loop through instance variables in a class in pyqt5. I came by the command setattr(). But unfortunately in my situation the goal is to -> self.variable.setpixmap() rather than self.variable = set.pixmap(). Is there a way to do this?

CodePudding user response:

It seems that you are trying to get a method by name (as a string) then invoke that. You can get the method with getattr(self, variable), then access other attributes and invoke it as any other method.

For example:

getattr(self, variable).setpixmap()

CodePudding user response:

value = getattr(self, 'variable_name')
value.setpixmap()
  • Related