A technique from a chatbot

Liste des GroupesRevenir à cl python 
Sujet : A technique from a chatbot
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 02. Apr 2024, 18:18:16
Autres entêtes
Organisation : Stefan Ram
Message-ID : <chatbot-20240402181409@ram.dialup.fu-berlin.de>
  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 )
   
  . No jumps anymore, yet the loop is aborted on the first hit
  (if I guess correctly how its working).
 
  And it is this combination of "next", a generator, and "None" that
  the chatbot showed me when I asked him how to get the first component
  of a list that matches a condition!
 
  PS: Let's verify the earliness of the exit out of the loop:
 
  Main.py
 
def list_():
    list__ =[ 'delta', 'epsilon', 'zeta', 'eta', 'theta' ]
    for entry in list__:
        print( f'Now yielding {entry}.' )
        yield entry

def first_word_beginning_with_e( list_ ):
    return next( ( word for word in list_() if word[ 0 ]== 'e' ), None )

print( first_word_beginning_with_e( list_ ))

   sys.stdout
  
Now yielding delta.
Now yielding epsilon.
epsilon

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