Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Newsgroups: comp.lang.python Subject: RE: A technique from a chatbot Date: Wed, 3 Apr 2024 01:27:00 -0400 Lines: 153 Message-ID: References: <7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net> <01d501da8587$8dd66c50$a98344f0$@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de i1PY9bv7wx94MInLyKlEMwj0EqsdEoVmuhwjzoadnDWA== Cancel-Lock: sha1:thtUwIQdqdNAHMswor+6/+nqCwI= sha256:cIKUwWlZEVDDTGh0eA8/fTSPouc+Akzi1HiMuOECRNs= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org Authentication-Results: mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=gmail.com header.i=@gmail.com header.b=EKY0Vm5k; dkim-adsp=pass; dkim-atps=neutral X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'argument': 0.04; 'def': 0.04; '3.8': 0.05; 'fairly': 0.05; 'variable': 0.05; 'loop': 0.07; 'matching': 0.07; 'modification': 0.07; 'ram': 0.07; 'string': 0.07; 'suggestion': 0.07; 'else:': 0.09; 'received:108': 0.09; 'received:mail-qk1-x72b.google.com': 0.09; 'url:mailman': 0.15; 'that.': 0.15; '"not': 0.16; "'e'": 0.16; '2024': 0.16; 'compiled': 0.16; 'confused': 0.16; 'context.': 0.16; 'executed': 0.16; 'explaining': 0.16; 'found,': 0.16; 'found.': 0.16; 'goto': 0.16; 'jumps': 0.16; 'none)': 0.16; 'nothing.': 0.16; 'variable,': 0.16; 'variant': 0.16; 'ways.': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'python': 0.16; 'instead': 0.17; "can't": 0.17; 'message- id:@gmail.com': 0.18; 'pm,': 0.19; 'to:addr:python-list': 0.20; 'all,': 0.20; 'maybe': 0.22; 'returns': 0.22; "what's": 0.22; 'version': 0.23; 'code': 0.23; 'skip:p 30': 0.23; 'list,': 0.24; 'skip:- 10': 0.25; 'url-ip:188.166.95.178/32': 0.25; 'url- ip:188.166.95/24': 0.25; 'depends': 0.25; 'url:listinfo': 0.25; 'programming': 0.25; 'url-ip:188.166/16': 0.25; 'pattern': 0.26; 'stefan': 0.26; 'else': 0.27; 'function': 0.27; 'done': 0.28; 'example,': 0.28; 'url-ip:188/8': 0.31; "doesn't": 0.32; 'fine.': 0.32; 'guess': 0.32; 'python-list': 0.32; 'but': 0.32; 'there': 0.33; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34; 'running': 0.34; 'complex': 0.35; 'runs': 0.35; 'from:addr:gmail.com': 0.35; 'target': 0.36; 'people': 0.36; 'necessarily': 0.37; 'subject:from': 0.37; "it's": 0.37; 'read': 0.38; 'least': 0.39; 'single': 0.39; 'text': 0.39; 'list': 0.39; 'use': 0.39; 'break': 0.39; 'still': 0.40; 'beginning': 0.40; 'difficult': 0.40; 'match': 0.40; 'should': 0.40; 'including': 0.60; 'initial': 0.61; 'above': 0.62; 'from:': 0.62; 'to:': 0.62; 'finally': 0.62; 'gain': 0.62; 'come': 0.62; 'simply': 0.63; 'complete': 0.64; 'personal': 0.64; 'clear': 0.64; 'full': 0.64; 'deliver': 0.64; 'opinion': 0.64; 'others,': 0.64; 're:': 0.64; 'similar': 0.65; 'look': 0.65; 'well': 0.65; 'less': 0.65; 'let': 0.66; 'improve': 0.66; 'lead': 0.67; 'shows': 0.67; 'smart': 0.67; 'that,': 0.67; 'counter': 0.69; 'it:': 0.69; 'skip:w 20': 0.69; 'solutions': 0.70; 'conditions': 0.70; 'skip:f 30': 0.71; 'longer': 0.71; 'read,': 0.75; 'skip:f 20': 0.75; 'supposed': 0.76; 'sent:': 0.78; 'returned': 0.81; 'more.': 0.82; 'points': 0.84; 'eastern': 0.84; 'know?': 0.84; 'picks': 0.84; 'solutions,': 0.84; 'statement.': 0.84; 'return,': 0.91; 'underneath': 0.91; 'immediate': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712122024; x=1712726824; darn=python.org; h=content-language:thread-index:content-transfer-encoding :mime-version:message-id:date:subject:in-reply-to:references:to:from :from:to:cc:subject:date:message-id:reply-to; bh=tVY2ViajLa0J9R3pq4ZG6XBuwktyQHPJonyosD5gAgY=; b=EKY0Vm5knYQEyIpSz7sQjVt12/XG8mUyZFGP/aZCp/Om7ePg3IcQ2yM5O0RpIvAEgc FsBcphhnLhEXT7bjrEyBO2YJho4NbIbENfHfBRIGdM8t7icbGdWLeGOCV4OIviiURRhX 2e81MP8IqcfQVK+iatPY3jspwEo49diRwqbuL6G8s6yRv9orJTatzhrJx5cngLy+t5Ri fS0/G+IHsBkxn0/D4tkbusE5mDIZ6zD2eA7COiWrg1yEVWRpkZ3eVNF5BV5/l5gW9bI7 gGedQgL7pUNJwjikiLFb7oOq4fSIg6pxeIrDLwppXr90UCZCk9vqPyky0hdKI47S6ijF G+gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712122024; x=1712726824; h=content-language:thread-index:content-transfer-encoding :mime-version:message-id:date:subject:in-reply-to:references:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tVY2ViajLa0J9R3pq4ZG6XBuwktyQHPJonyosD5gAgY=; b=t6qmEZI9m+laoAaD8Hz2u2xBOkQYHlO0dvupP9tRAa7DU9KVmMOHmoDLnoyBPkEkGA apYPuw+iSnwU10KsDTTV6OzqJ1NL6zqRbiG8Pxzoj77Jd2+7ICE1iP6O47fGRedKLvzd 5yC5GWMijmcks4d0Pywp/FQtJ5OHsh1vj4tnDF6G07SPKj8H1UkSXzQkm/flV6yeyM8W KeNG6fdgW/9Gs98D/0v16poXvPLKca75tOlqCbY6vWqX7V2VDWxGEUh9xZhqYbYlWTlc qn9ZpaTXe1donsD1qb2fzxLNGoJzvmCoud3Xj+VxUmDTECE9PAeeKvNWeCDVRATd762j V8jQ== X-Gm-Message-State: AOJu0Ywzvu1p/slkRXGOnTWOgaFKSBQQUw+Tj6Df1Jp+QpBU1+wf6ExO fvjvPXrlnNP0so2tTe8GklhUMAIiW6l8Tvj2tQYaK5JVv7yFemocbjIdFnPz X-Google-Smtp-Source: AGHT+IGbL7MquukcGkTzs+Es+0A7PyvLLxxb0O3VAU6oQRL6e9uWciP5eGXKxz12F7/jj0Zz0GjcZw== X-Received: by 2002:ae9:ee15:0:b0:789:d43b:3702 with SMTP id i21-20020ae9ee15000000b00789d43b3702mr13999614qkg.59.1712122023598; Tue, 02 Apr 2024 22:27:03 -0700 (PDT) In-Reply-To: <7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net> X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQG2as0aVd2VIulEmuXRHp684y8JCQJBvrQcAfnDloGxfFEv4A== Content-Language: en-us X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.39 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <01d501da8587$8dd66c50$a98344f0$@gmail.com> X-Mailman-Original-References: <7d38d9e2-78fb-43af-971f-e0d4afb8b039@tompassin.net> Bytes: 12060 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 ]=3D=3D '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 =3D ["eastern", "Western", "easter"] NorEaster =3D ["North", "West", "orient"] def first_word_beginning_with_e( list_ ): return(result[0] if (result :=3D [word for word in list_ if = word[0].lower() =3D=3D '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 -----Original Message----- From: Python-list = On Behalf Of = Thomas Passin via Python-list Sent: Tuesday, April 2, 2024 3:31 PM To: python-list@python.org Subject: Re: A technique from a chatbot 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 ]=3D=3D '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 ]=3D=3D 'e': return word ========== REMAINDER OF ARTICLE TRUNCATED ==========