I'm a newby and just learned about classes in python3. Now i wrote an exchanchable code to practice a little bit, and wonder, if there's a way to make this code "smarter".
dic = {
"1": ["I", "am", "the", "1.", "entry"],
"2": ["I", "am", "the", "2.", "entry"],
"3": ["I", "am", "the", "3.", "entry"],
"4": ["I", "am", "the", "4.", "entry"],
"5": ["I", "am", "the", "5.", "entry"],
}
class words:
def __init__(self, word1, word2, word3, word4, word5):
self.word1 = word1
self.word2 = word2
self.word3 = word3
self.word4 = word4
self.word5 = word5
for i in dic:
globals()[f"entry{i}"] = words(dic[i][0],dic[i][1],dic[i][2],dic[i][3],dic[i][4])
print(entry3.__dict__)
The dic above is meant to define the start-values for each instance of the class. Later on there can be ways to modify the attributes for these instances, but that's not important right now.
Like this is written, it's pretty unflexible. If I wanna add another element into an dic-element like this
"1": ["I", "am", "the", "1.", "entry", "!"],
I would have to add a parameter in init like
__init__(self,word1,...,word5,word6)
and also add into init-function
self.word6 = word6
and also add parameter-entry in the definition of the instances like
globals()[f"entry{i}"] = words(Vz[i][0],...,Vz[i][4],Vz[i][5])
Is there any way to automate this process (like looping through them), so if I wanna add another start value for the instances of the class, I don't have to add all these things too?