A technique from a chatbot
Piergiorgio Sartor
piergiorgio.sartor.this.should.not.be.used at nexgo.REMOVETHIS.de
Tue Apr 2 13:47:59 EDT 2024
On 02/04/2024 19.18, Stefan Ram wrote:
> Some people can't believe it when I say that chatbots improve
> my programming productivity. So, here's a technique I learned
> from a chatbot!
>
> It is a structured "break". "Break" still is a kind of jump,
> you know?
>
> So, what's a function to return the first word beginning with
> an "e" in a given list, like for example
>
> [ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
>
> ? Well it's
>
> def first_word_beginning_with_e( list_ ):
> for word in list_:
> if word[ 0 ]== 'e': return word
>
> . "return" still can be considered a kind of "goto" statement.
> It can lead to errors:
>
> def first_word_beginning_with_e( list_ ):
> for word in list_:
> if word[ 0 ]== 'e': return word
> something_to_be_done_at_the_end_of_this_function()
>
> The call sometimes will not be executed here!
> So, "return" is similar to "break" in that regard.
>
> But in Python we can write:
>
> def first_word_beginning_with_e( list_ ):
> return next( ( word for word in list_ if word[ 0 ]== 'e' ), None )
Doesn't look a smart advice.
> . No jumps anymore, yet the loop is aborted on the first hit
First of all, I fail to understand why there
should be no jumps any more.
It depends on how "return" and "if" are handled,
I guess, in different context.
Maybe they're just "masked".
In any case, the "compiler" should have just
done the same.
> (if I guess correctly how its working).
Second, it is difficult to read, which is bad.
The "guess" above is just evidence of that.
My personal opinion about these "chatbots", is
that, while they might deliver clever solutions,
they are not explaining *why* these solutions
should be considered "clever".
Which is the most important thing (the solution
itself is _not_).
bye,
--
piergiorgio
More information about the Python-list
mailing list