Cannot unpack non-iterable int object
12,543
To make a getitem that returns the contents of each of the member variables given a key that is the same as the name of that member, you could have the class store the information in a dict and have your get_item return from that dict. E.g.:
def __init__(self, name = 'Jud', author='Jill', category='YA'):
self.elements = {'name':name,'author':author,'category':category}
def __getitem__(self, key):
return self.elements[key]
Then with an instance of the class you should be able to do things like:
aBook['name']
aBook['author']
Author by
Umut K.
Updated on June 15, 2022Comments
-
Umut K. almost 2 years
I need to use
__getitem__
for all 7 arguments in my class but__getitem__
won't let me so I tried to use atuple
but I keep getting this error:TypeError: cannot unpack non-iterable int object
What should I do?
def __getitem__(self, key): name,author,category,Isbn_number,printing_house,date,number_of_order = key return (self.name,self.author,self.category,self.Isbn_number,self.printing_house,self.date,self.number_of_order)
-
pangyuteng over 5 years
-
Umut K. over 5 yearsI dont get it ı mean ı understand(kinda) how getitem works but ı need to use getitem method for getting 7 arg in key cause ı want tu use a sorting function for my objects so ı need my objects to return as keys
-
pangyuteng over 5 yearsi recommend just creating your own custom
get
method, no need to pass in any arguments to the method nor even follow the python__getitem__
convention. -
Umut K. over 5 yearsbut ı dont know how yo use a get method but ı tried and then ı figured out my args are all str so dict.get cant receive them
-