I have come across a for-loop that is unusual to me. What does method mean in this for-loop?
for method, config in self.myList.items():
Range with three parameters: start, end, step.
Contrary to other languages, in Smalltalk a for-loop is not a language construct but defined in the class Number as a method with two parameters, the end value and a closure, using self as start value.
No. Using enumerate() we'll get an enumerate object. Save this answer.
items() is a method used on python dictionaries to return an iterable holding tuples for each of the dictionary's keys and their corresponding value.
In Python you can unpack lists and tuples into variables using the method you've shown.
e.g.:
item1, item2 = [1,2]
# now we have item1 = 1, item2 = 2
Therefore, assuming self.myList is a dict, method and config would relate to the key and value in each tuple for that iteration respectively.
If self.myList is not a dict, I would assume it either inherits from dict or it's items() method is similar (you would know better).
It's unpacking a tuple returned from the items() call into the method and config variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With