Python Forum
Is it possible to loop through class attributes via string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to loop through class attributes via string?
#1
Hey guys, I'm stumped on this issue.
I currently am referencing a UI file built in pyqt, and though it wise to create a while loop to iterate through the button commands. However, it seems to be pretty stubborn, and i'm not sure how to go about it.
iconEnumerator = 0
#This works, since there is a PoseSlot_32 attribute. The button slots go from 0-32
self.PoseSlot_32.clicked.connect(self.printonetwoThree2)

#This doesn't work (looping)
while iconEnumerator >= 32:
	"self.PoseSlot_" + str(iconEnumerator) + ".clicked.connect(self.printonetwoThree2)"
	iconEnumerator += 1
what is difficult is that i've tried
#setAttr(self, "PoseSlot_32.clicked.connect()", self.printonetwoThree2)
stringPoseAttribute = "self.PoseSlot_" + str(iconEnumerator) + ".clicked.connect(self.printonetwoThree2)"
#eval(stringPoseAttribute)
and #getAttr Doesn't seem to be useful since it keeps returning invalid object
Reply
#2
What is your actual purpose on doing this determines the result.

From the object of the class
>>> class Klass:
...     def __init__(self):
...             self.var = 1
...             self.pot = 'test'
... 
>>> obj = Klass()
>>> obj.__dict__
{'var': 1, 'pot': 'test'}
Recommended Tutorials:
Reply
#3
I'm currently trying to create a button gallery, however the button's functionality is triggered but a button attribute function. The reason for iteration through all these buttons. IE
self.PoseSlot_1.clicked.connect(self.printonetwoThree2)
self.PoseSlot_2.clicked.connect(self.printonetwoThree2)
self.PoseSlot_3.clicked.connect(self.printonetwoThree2)
self.PoseSlot_4.clicked.connect(self.printonetwoThree2)
...
is to create a default function on the click command if there is a null value.

I just figure I could make a loop so i didn't have to copy and paste the code like above

Oh yea also, All of the attributes are already generated through the UI file.* "PoseSlot_ ..."
Reply
#4
Completely misunderstood the syntax of getAttr. Issue resolved
getattr(self, "PoseSlot_1").setIcon(QtGui.QIcon(self.jsonFolderPath + "/icon/" + self.jsonTemplateData.values()[0][0]["icon"]))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 891 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 989 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Loop through a list of string oldtrafford 4 1,423 Mar-24-2022, 05:30 PM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Distinguishing different types of class attributes Drone4four 4 2,054 Feb-21-2022, 06:34 PM
Last Post: deanhystad
  Loop through a list of string oldtrafford 3 1,658 Feb-15-2022, 04:42 PM
Last Post: snippsat
  loop for dynamic cut string - cleaner way? korenron 4 1,877 Nov-22-2021, 02:30 PM
Last Post: korenron
  I am trying to reverse a string using loop codinglearner 4 2,128 Sep-28-2021, 10:46 PM
Last Post: Pedroski55
  Calls to Attributes of a Class SKarimi 3 3,338 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  Convert string to JSON using a for loop PG_Breizh 3 2,897 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020