On Wed, 1 Jun 2022 at 03:34, Aaron L via Python-ideas
<[email protected]> wrote:
>
> Thanks for your reply.
>
> > What's the advantage?
>
> I brought this up thinking about explicitness and readability. Say you want
> to figure out what this function is doing:
>
> ````
> def foo() -> t.Iterator[T]:
> [... 300 lines of code]
> ```
>
> Is this a generator function? I'd argue that whether it's a generator
> function or not is fundamental to being able to read it. The type hint alone
> doesn't tell you whether you're looking at a generator function or not - it
> might just construct and return an iterator.
>
Does it actually matter whether it's a generator, or returns some
other type of iterable? What's the difference between these two
functions:
def enumerate_spam(n):
yield "spam 1"
yield "spam 2"
yield "spam 3"
yield n
yield "the rest of the spam"
def enumerate_default_spam():
return enumerate_spam("default")
Technically, one of these is a generator, and one is not. But the
return value from both of them is a generator object. You can send it
values, get values back, all the things you can do with a generator.
How fundamental is it that THIS function is a generator, rather than
simply that it returns an iterator (or that it returns a
generator/coroutine object, etc)?
If your function is really just named "foo" and has 300 lines of code,
you have other problems. Normally, the function's name should tell you
a lot. In your case, you seem to also have a return type hint, which
tells you a bit more, so that ought to be sufficient?
Maybe that's not sufficient for your codebase. Well, that's what
docstrings and decorators and code comments are for :)
ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/7DC4LRAJXIIAPH43NCFJJIAKUG4M6RPD/
Code of Conduct: http://python.org/psf/codeofconduct/