Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"if': 0.04; 'attributes': 0.05; 'terry': 0.07; 'python': 0.07; 'agree,': 0.09; 'worse': 0.09; '>>>': 0.12; 'wrote:': 0.14; 'do!': 0.16; 'from:addr:awilliam': 0.16; 'from:addr:whitemice.org': 0.16; 'from:name:adam tauno williams': 0.16; 'len(x)': 0.16; 'list...': 0.16; 'message-id:@linux-yu4c.site': 0.16; 'object)': 0.16; 'received:72.14.190': 0.16; 'received:72.14.190.87': 0.16; 'received:mail.wmmi.net': 0.16; 'received:wmmi.net': 0.16; 'reedy': 0.16; 'reply-to:addr:awilliam': 0.16; 'reply- to:addr:whitemice.org': 0.16; 'such,': 0.16; 'ignore': 0.16; 'meant': 0.18; 'awesome': 0.19; 'subject:list': 0.22; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'exist,': 0.23; 'says': 0.25; 'received:72.14': 0.27; 'work.': 0.27; "doesn't": 0.28; '(as': 0.29; 'fri,': 0.29; 'fine.': 0.29; 'list': 0.30; 'does': 0.31; 'it.': 0.31; 'fact': 0.31; 'supposed': 0.31; 'to:addr:python- list': 0.32; '...': 0.32; 'words,': 0.33; 'received:192.168.1': 0.34; 'received:192': 0.34; 'there': 0.35; 'implies': 0.35; 'test.': 0.35; 'received:192.168': 0.37; 'some': 0.37; 'way.': 0.37; 'case': 0.37; 'but': 0.38; 'anything': 0.38; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'empty': 0.40; 'might': 0.40; 'you.': 0.61; 'addition': 0.62; 'noise': 0.68; 'reply-to:no real name:2**0': 0.72; 'header:Reply- To:1': 0.72; 'luck.': 0.84 Subject: Re: checking if a list is empty From: Adam Tauno Williams To: python-list@python.org In-Reply-To: <9hYwp.5805$xo2.3333@newsfe07.iad> References: <9hYwp.5805$xo2.3333@newsfe07.iad> Content-Type: text/plain; charset="UTF-8" Date: Fri, 06 May 2011 16:05:09 -0400 Mime-Version: 1.0 X-Mailer: Evolution 2.32.1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: awilliam@whitemice.org 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: 31 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304712385 news.xs4all.nl 41102 [::ffff:82.94.164.166]:59969 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4863 On Fri, 2011-05-06 at 14:49 -0500, harrismh777 wrote: > Terry Reedy wrote: > >>> (2) if not li: > >> This is fine. > > This is the intended way. Anything in addition is extra noise and wasted > > calculation. In other words, let Python do the boilerplate work for you. > I agree, but I don't like it. +1 This is the Python reality-distortion-field at work. Motto#1: Python is all about readability! Motto#2: Crytic code is awesome if it is Pythoncally cryptic! I'd never accept code like "if not x" as an empty test. > ... if not li says nothing about what li is supposed to 'be' and > implies in any case that li does not exist, or worse is some kind of > boolean. > li is in fact an empty list [] and will identify as such, and of > course (as a list object) has all of the attributes and methods of a list... > Why not have a list method that makes this more explicit: > if not li.emptylist() > if not li.empty([]) > there might be others... Good luck. Just code - # You can immediately tell what this is meant to do! if len(x) == 0: - and ignore the Pythonistas [they're nuts; that x.count() doesn't work is amazingly stupid].