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 : 03. Apr 2024, 12:50:55
Autres entêtes
Message-ID : <mailman.62.1712153666.3468.python-list@python.org>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 4/3/2024 1:27 AM, AVI GROSS via Python-list wrote:
I am a tad confused by a suggestion that any kind of GOTO variant is bad. The suggestion runs counter to the reality that underneath it all, compiled programs are chock full of GOTO variants even for simple things like IF-ELSE.
 Consider the code here:
 
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()
 If instead the function initialized a variable to nothing useful and in the loop if it found a word beginning with e and it still contained nothing useful, copied it into the variable and then allowed the code to complete the loop and finally returned the variable, that would simply be a much less efficient solution to the problem and gain NOTHING. There are many variants you can come up with and when the conditions are complex and many points of immediate return, fine, then it may be dangerous. But a single return is fine.
 The function does have a flaw as it is not clear what it should do if nothing is found. Calling a silly long name does not necessarily return anything.
 Others, like Thomas, have shown other variants including some longer and more complex ways.
 A fairly simple one-liner version, not necessarily efficient, would be to just use a list comprehension that makes a new list of just the ones matching the pattern of starting with an 'e' and then returns the first entry or None. This shows the code and test it:
 text = ["eastern", "Western", "easter"]
 NorEaster = ["North", "West", "orient"]
 def first_word_beginning_with_e( list_ ):
   return(result[0] if (result := [word for word in list_ if word[0].lower() == 'e']) else None)
 print(first_word_beginning_with_e( text ))
print(first_word_beginning_with_e( NorEaster ))
 Result of running it on a version of python ay least 3.8 so it supports the walrus operator:
 eastern
None
The OP seems to want to return None if a match is not found.  If a Python function ends without a return statement, it automatically returns None.  So nothing special needs to be done.  True, that is probably a special case, but it suggests that the problem posed to the chatbot was not posed well.  A truly useful chatbot could have discussed many of the points we've been discussing.  That would have made for a good learning experience.  Instead the chatbot produced poorly constructed code that caused a bad learning experience.

[snip...]

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