Re: A technique from a chatbot

Liste des GroupesRevenir à cl python 
Sujet : Re: A technique from a chatbot
De : list1 (at) *nospam* tompassin.net (Thomas Passin)
Groupes : comp.lang.python
Date : 02. Apr 2024, 20:31:26
Autres entêtes
Message-ID : <mailman.58.1712110350.3468.python-list@python.org>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 4/2/2024 1:47 PM, Piergiorgio Sartor via Python-list wrote:
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
It's worse than "not a smart advice". This code constructs an unnecessary tuple, then picks out its first element and returns that. The something_to_be_done() function may or may not be called.  And it's harder to read and understand than necessary.  Compare, for example, with this version:
def first_word_beginning_with_e(target, wordlist):
     result = ''
     for w in wordlist:
         if w.startswith(target):
             res = w
             break
     do_something_else()
     return result
If do_something_else() is supposed to fire only if the target is not found, then this slight modification will do:
def first_word_beginning_with_e(target, wordlist):
     result = ''
     for w in wordlist:
         if w.startswith(target):
             res = w
             break
     else:
         do_something_else()
     return result
[Using the "target" argument instead of "target[0]" will let you match an initial string instead of just a the first character].

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,
 

Date Sujet#  Auteur
2 Apr 24 * A technique from a chatbot15Stefan Ram
2 Apr 24 +* Re: A technique from a chatbot12Piergiorgio Sartor
2 Apr 24 i+* Re: A technique from a chatbot8Thomas Passin
4 Apr 24 ii`* Re: A technique from a chatbot7Mark Bourne
4 Apr 24 ii +* Re: A technique from a chatbot2<avi.e.gross
5 Apr 24 ii i`- Re: A technique from a chatbot1Mark Bourne
4 Apr 24 ii +- Re: A technique from a chatbot1Thomas Passin
5 Apr 24 ii `* Re: A technique from a chatbot3Stefan Ram
5 Apr 24 ii  +- Re: A technique from a chatbot1Stefan Ram
5 Apr 24 ii  `- Re: A technique from a chatbot1Mark Bourne
3 Apr 24 i+- Re: A technique from a chatbot1<avi.e.gross
3 Apr 24 i+- Re: A technique from a chatbot1Thomas Passin
3 Apr 24 i`- Re: A technique from a chatbot1<avi.e.gross
3 Apr 24 +- Re: A technique from a chatbot1Pieter van Oostrum
3 Apr 24 `- Re: A technique from a chatbot1Michael F. Stemper

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal