[Python-ideas] + operator on generators

Koos Zevenhoven k7hoven at gmail.com
Wed Jun 28 10:01:47 EDT 2017


On Wed, Jun 28, 2017 at 3:18 PM, Sven R. Kunze <srkunze at mail.de> wrote:

> On 28.06.2017 14:00, Koos Zevenhoven wrote:
>
> The programmer needs to be well aware of whether the resulting object is a
> Sequence or 'just' a generator.
>
>
> Could you elaborate more on **why**?
>
>
​For a moment, I was wondering what the double emphasis was for, but then I
realized you are simply calling `statement.__why__()`​ directly instead of
the recommended `spoiler(statement)`. But sure, I just got on vacation and
I even found a power extension cord to use my laptop at the pool, so what
else would I do ;).

It all depends on what you need to do with the result of the concatenation.
When all you need is something to iterate over, a generator-like thingy is
fine. But when you need something for indexing and slicing or len etc., you
want to be sure that that is what you're getting. But maybe someone passed
you an argument that is not a sequence, or you forgot if a function returns
a sequence or a generator. In that case, you want the error right away,
instead of from some completely different piece of code somewhere that
thinks it's getting a sequence. I don't think Python should depend on a
static type checker to catch the error early. After all, type hints are
optional.

PS: I consider this proposal to be like allowing adding floats and ints
> together. If I don't know if there was a float in the sum, don't know if my
> result will be a float.
>

​Not to say that the float/int case is never problematic, but the situation
is still different. Often when a float makes any sense, you can work with
either floats or ints and it doesn't really matter. But if you specifically
*need* an int, you usually don't call functions that return floats. But if
you do use division etc., you probably need to think about
floor/ceil/closest anyway. And yes, there have probably been Python 2->3
porting bugs where / division was not appropriately replaced with //.

But regarding containers, it often makes just as much sense for a function
to return a generator as it does to return a sequence. The name or purpose
of a function may give no hint about whether an iterable or sequence is
returned, and you can't expect everyone to prefix their function names with
iter_ and seq_ etc. And it's not just function return values, it's also
arguments passed into your function.
​​
​-- Koos​


-- 
+ Koos Zevenhoven + http://twitter.com/k7hoven +
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170628/e4b6349f/attachment.html>


More information about the Python-ideas mailing list