[Python-ideas] Does jargon make learning more difficult?

Chris Angelico rosuav at gmail.com
Wed Aug 22 19:07:13 EDT 2018


On Thu, Aug 23, 2018 at 9:02 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Rhodri James wrote:
>>
>> This, by the way, is why think using the same syntax for function
>> definition and generator definition was a mistake.
>
>
> I think I remember arguing the same thing back when generators
> were being devised.
>
> But there are arguments the other way too. From the outside,
> a generator is just a function that returns an iterator --
> the fact that it works by yielding is an implementation detail.
>
> When you write an ordinary function that returns an iterator, the
> fact that it returns an iterator needs to be conveyed some other
> way, such as by its name or documentation. if you can do that
> for an ordinary function, you can do it for a generator just as
> well -- and probably should, so that you can tell what it returns
> without having to look at its definition.
>
> If the name of the function indicates that it returns an iterator,
> or there's a docstring at the top what says so, then you won't
> be surprised when you find a yield in it somewhere.

def gen1(x):
    return iter(range(x))

def gen2(x):
    i = 0
    while i < x:
        yield i
        i += 1

def gen3(x):
    if i % 2: return gen1(x)
    else: return gen2(x)

Which of these three are generator functions? Technically only one of
them is... but how would it make any difference?

ChrisA


More information about the Python-ideas mailing list