Skip to main content
13 events
when toggle format what by license comment
Dec 20, 2015 at 20:42 history edited Barry CC BY-SA 3.0
added 293 characters in body
Dec 20, 2015 at 20:28 comment added martineau It should be noted that the yield from <expression> form wasn't added until Python 3.3 according to the documentation.
Dec 20, 2015 at 20:22 comment added BenC You could also use .values() instead of .items() to avoid the need for _ -- really just up to you.
Dec 20, 2015 at 20:08 vote accept aaragon
Dec 20, 2015 at 19:58 history edited Barry CC BY-SA 3.0
added 106 characters in body
Dec 20, 2015 at 19:58 comment added Barry @aaragon _ is for things you don't care about - just name them _ to indicate that they won't be used again. No idea why you would need __call__. Just look up what yield means and how it can be used.
Dec 20, 2015 at 19:56 comment added aaragon Ok I see. Now, I'm new to Python so this code seems quite complex. I don't understand the _ in the for loops. Also, this generator that is returned is iterating over the whatever dictionary, but in my data structure I have a list of whatever dictionaries. So if I want to go to a lower dimension, then that means I have to change the whatever dictionary. Should I store a variable to determine this and override __call__ to change this variable as I've done it above?
Dec 20, 2015 at 19:54 comment added Barry @aaragon Yep. __iter__ can return a generator.
Dec 20, 2015 at 19:53 comment added aaragon I think I like this last suggestion but I'm not sure I understand exactly how to do that. I create a class, say Custom, and I override the __iter__ method and put inside what you wrote. That's it?
Dec 20, 2015 at 19:51 history edited Barry CC BY-SA 3.0
added 309 characters in body
Dec 20, 2015 at 19:50 comment added Barry @aaragon You have for c in iter_over_custom(custom). Or you can make custom its own class and define __iter__ as what I wrote iter_over_custom to be (__iter__ can yield).
Dec 20, 2015 at 19:47 comment added aaragon But using this approach I still don't have the for c in custom: syntax, do I? Nor I can't iterate over lower-dimensional dictionaries?
Dec 20, 2015 at 19:45 history answered Barry CC BY-SA 3.0