Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; ';-)': 0.03; 'skip:[ 20': 0.04; 'subject:Python': 0.06; 'abuse': 0.07; 'assignment': 0.07; 'dev': 0.07; 'test,': 0.07; 'subject: [': 0.09; 'subject:language': 0.09; 'times,': 0.14; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'stumbled': 0.16; 'language': 0.16; 'code,': 0.22; "i've": 0.25; 'said,': 0.30; 'languages': 0.32; "i'd": 0.34; 'something': 0.35; 'done': 0.36; 'doing': 0.36; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'offer': 0.62; 'more': 0.64; 'production': 0.68; 'receive': 0.70; 'friends': 0.81; 'received:50.22': 0.84; 'subject:NOT': 0.84; 'ugly,': 0.84 Date: Thu, 27 Mar 2014 11:08:56 -0500 From: Tim Chase To: python-list@python.org Subject: Python language hack for C-style programmers [DO NOT USE!] :-) X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Importance: low X-Priority: 4 (Low) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395936527 news.xs4all.nl 2850 [2001:888:2000:d::a6]:51586 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69202 Multiple times, I've seen someone want something like what C-style languages offer where assignment is done in a test, something like if (m = re.match(some_string)): do_something(m) So when I stumbled upon this horrific atrocity of language abuse and scope leakage, I thought I'd share it. if [m for m in [regex.match(some_string)] if m]: do_something(m) And presto, assignment in an if-statement. It even "works" in while-statements too: while [m for m in [regex.match(some_string)] if m]: some_string = do_something(m) That said, it's ugly, far more opaque/inefficient than the traditional m = regex.match(some_string) if m: do_something(m) and if I ever caught someone on my dev teams doing this in production code, their backside would receive a stern conversation with my footwear. Friends don't let friends program C in Python. ;-) -tkc