Before I say a word, let me thank the community for being the authoritative location for my programming queries as of recent. And pretend those compliments weren't expressed using words. Anyway, the law of probability dictated that I stumble across something I couldn't find using the versatile search bar, so I've decided to explicitly ask for the first time. Maybe I just wasn't searching using Pythonic-enough lingo. Or perhaps I suck at Googling/Stackoverflowing. Regardless...
I'm toying with Python coroutines and generators. From what I can gather, you can do anything a generator-comprehension can with producer coroutines, albeit more verbosely. I'm currently using Python 3, although any answers regarding Python 2 as well wouldn't go a miss.
So I'm assuming the following code fragments are equivalent:
one_to_three = (num for num in range(1, 4))
...
def one_to_three():
for num in range(1, 4):
yield num
one_to_three_gen = one_to_three()
It works on my Python installation. If I ignore the redundancy-so-common-in-examples featured in that code, I see that the generator comprehension maps easily to the generator made by the producer coroutine. Being Dr. Pragmatic, I tried mapping the same concept to dicts, given that dict comprehensions already exist, with me thinking these two would be equivalent:
one_to_three_doubles = {num : num * 2 for num in range(1, 4)}
...
def one_to_three_doubles():
for num in range(1, 4):
yield num : num * 2
one_to_three_doubles_gen = one_to_three_doubles()
The first one works, but the second does not. It flags a syntax error on the colon on the 3rd line.
Now, either I'm slipping up very slightly on the syntax, or I have a massive misunderstanding of how producer coroutines work. I suspect it's failing for the same reason you can't make a coroutine return a list as opposed to a generator, but I don't really know.
So yeah, a fix to that error is basically what I'm asking for; thanks in advance. I'd prefer a answer that tells me the answer as opposed to giving me a whole new way of achieving the result, but obviously if it's the only way...

所有评论(0)