Live data from Hacker News

The Python yield keyword explained

stackoverflow.com

21–30 of 53 posts

Re: The Python yield keyword explained

#21
post #5

Earlier quoted context omitted.

I use yield and generators regularly. They just make sense, but I never knew what a coroutine is in an academic sense

This is why you study CS at university before you start work as a programmer. Otherwise you end up being amazed by the basic CS concepts, or worse, you end up reinventing them badly.

It is pretty awesome to be privileged and living in an affluent country, eh?

Re: The Python yield keyword explained

#22
post #8

Earlier quoted context omitted.

Does studying CS at university make people as insufferably grumpy and unhelpful as you?

Come on, I've got a reasonable point here. People post on forums asking these questions which should have been answered during their educations. If civil engineers had a forum site and someone asked "how do I calculate the load a simple beam", people would say, what the fuck are you doing in your job if you don't know that? Who is employing you and why? Where did you go to college? Who was your tutor because next tim…

Python has a lot of users who are not programmers by profession, but rather mathematicians or scientists or other professions where Python is an excellent tool for getting their main job done.

So assuming that only well-educated programmers can have questions about a programming language is asinine.

My university education didn't cover coroutines either.

Re: The Python yield keyword explained

#23
post #18

Earlier quoted context omitted.

It's precisely all the help you need to go and look up your notes on coroutines from college. You did go to college didn't you? Or are you an anti-college hipster, suddenly finding the limitations of that approach.

stch - your account has been hell banned, for nearly a whole year.

What does that mean/how does it affect me? And howd that happen?

Re: The Python yield keyword explained

#24
post #8

Earlier quoted context omitted.

Does studying CS at university make people as insufferably grumpy and unhelpful as you?

Come on, I've got a reasonable point here. People post on forums asking these questions which should have been answered during their educations. If civil engineers had a forum site and someone asked "how do I calculate the load a simple beam", people would say, what the fuck are you doing in your job if you don't know that? Who is employing you and why? Where did you go to college? Who was your tutor because next tim…

SO is not a site for software engineers, it's a site for programmers, regardless of their level of knowledge, education or age.

And by the way, I'd rather work with fellow programmers who don't know what a coroutine is than with an insufferable elitist like your posts make you out to be.

Re: The Python yield keyword explained

#25
post #18

Earlier quoted context omitted.

It's precisely all the help you need to go and look up your notes on coroutines from college. You did go to college didn't you? Or are you an anti-college hipster, suddenly finding the limitations of that approach.

stch - your account has been hell banned, for nearly a whole year.

(replying to myself because I can't reply to stch directly):

What does that mean/how does it affect me?

It means that only people who have specifically turned "showdead" on can see your posts. No one can reply to you posts directly. Your upvotes do nothing and don't count. But crucially, to you, everything seems completely normal.

And howd that happen?

An administrator decided that your first ever comment didn't fit the tone of this site (which it didn't, but well, you were new), so they decided to instantly and silently ban your account forever. So you have been commenting, for a year, and basically no one has seen your posts. I feel really bad about that and I hate this objectionable practise.

Re: The Python yield keyword explained

#26
post #5

Earlier quoted context omitted.

I use yield and generators regularly. They just make sense, but I never knew what a coroutine is in an academic sense

This is why you study CS at university before you start work as a programmer. Otherwise you end up being amazed by the basic CS concepts, or worse, you end up reinventing them badly.

It is one thing suggesting it is unwise to develop a new language without a CS background or broad and deep experience with other languages of some form as you might end up with something like PHP but there are plenty of ideas to develop that a CS background helps fairly little.

Generally curiosity and willingness to learn is worth more.

Re: The Python yield keyword explained

#27
post #25
post #18

Earlier quoted context omitted.

stch - your account has been hell banned, for nearly a whole year.

(replying to myself because I can't reply to stch directly): What does that mean/how does it affect me? It means that only people who have specifically turned "showdead" on can see your posts. No one can reply to you posts directly. Your upvotes do nothing and don't count. But crucially, to you, everything seems completely normal. And howd that happen? An administrator decided that your first ever comment didn't fit…

Wow, that's pretty ridiculous. You'd figure after a few months they'd tell me, right? Is that also why I get "Unknown or expired link" all the time and every page always takes 15-20 seconds to load? I noticed when I logged out that issue went away. I always just assumed the forum was really poorly designed. :o)

Re: The Python yield keyword explained

#28
post #5

Earlier quoted context omitted.

I use yield and generators regularly. They just make sense, but I never knew what a coroutine is in an academic sense

This is why you study CS at university before you start work as a programmer. Otherwise you end up being amazed by the basic CS concepts, or worse, you end up reinventing them badly.

FWIW I majored in computer science at a major school, though admittedly I was a shitty student. I spent more time building things than going to class.

Re: The Python yield keyword explained

#29
post #5

It's a coroutine. Not exactly revolutionary.

I use yield and generators regularly. They just make sense, but I never knew what a coroutine is in an academic sense

This does reflect very sadly on schools, and shows up when trying to explain things to programmers used to conventional languages, especially if you're trying to explain why C is fundamentally broken.

Roughly there are three fundamental control transfer operations: conditional jumps, subroutine calls, and coroutine calls. A subroutine is a slave, the caller is a master. With coroutines, the caller and callee are peers.

Coroutine calling is more fundamental and easier to program, but it isn't available in C.

The theory is about continuations. A continuation is just "the rest of the program". You can think of it as the program counter (PC). Subroutine calling works by passing a continuation to a routine, when the routine is finished it invokes the continuation. This is done by pushing the program counter on the stack, and the subroutine popping it with a return statement.

Coroutines work by exchange of control. When you call a coroutine you give it your current continuation to call when its ready. But also you do not call the coroutine at the beginning. You call it where it last left off: at its last continuation point.

A set of coroutines are usually called fibres. They represent interleaving of control. You can emulate them with pre-emptive threads and locks, but coroutines are synchronous and non-premptive.

Felix and Go both make heavy use of coroutines (called fthreads and goroutines). Iterators as in Python are a special case.

In general on today's badly designed CPU's you have to think of coroutines as requiring stack swapping. Threads do this which is why you can emulate coroutines with threads.

By far the most well known coroutine scheduler is .. the Operating System. Its basically a coroutine of applications. This is why you can read and write to files: the real world is event driven but callbacks are impossible to program with. So the operating control inverts the events by stack swapping so your application can be written as a master.

You think you're calling the OS, and the OS thinks its calling you. You're both masters. That's coroutines.

Python (and Felix) both have yield, but that's a special case. In general the model is reading and writing channels: yielding is just writing the "sole" channel and getting a function result is just reading it. A channel is basically a "place to swap stacks".

Re: The Python yield keyword explained

#30
What I find weird about the yield keyword is that it seems to effect the execution of code that comes before the statement, causing it not to execute until later.

For example:

  def createGenerator():
     print "aaa"

  mygen = createGenerator()
outputs:

aaa

Whereas

  def createGenerator():
    print "aaa"
    yield

  mygen = createGenerator()
Outputs nothing.
Post reply on HN