A technique from a chatbot

avi.e.gross at gmail.com avi.e.gross at gmail.com
Wed Apr 3 11:32:44 EDT 2024


Sadly, Thomas, this is not even all that new.

I have seen people do searches on the internet for how to do one thing at a
time and then cobble together some code that does something but perhaps not
quite what they intended. Some things are just inefficient such as reading
data from a file, doing some calculations, writing the results to another
file, reading them back in and doing more calculations and writing them out
again and so on. Yes, there can be value in storing intermediate results but
why read it in again when it is already in memory? And, in some cases, why
not do multiple steps instead of one at a time and so on.

How many people ask how to TEST the code they get, especially from an
AI-like ...?



-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Thomas Passin via Python-list
Sent: Wednesday, April 3, 2024 7:51 AM
To: python-list at python.org
Subject: Re: A technique from a chatbot

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...]

-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list