Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'plenty': 0.03; 'context': 0.04; 'instance': 0.05; 'memory.': 0.05; 'builtins': 0.07; 'subject:when': 0.07; 'type,': 0.07; 'variable,': 0.07; 'python': 0.08; '"run': 0.09; 'builtin': 0.09; 'foo': 0.09; 'throw': 0.09; 'examples': 0.11; 'library': 0.15; '16,': 0.15; "'file')": 0.16; 'common.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'grep': 0.16; 'makefile': 0.16; 'semanchuk': 0.16; 'subject:builtin': 0.16; 'suite;': 0.16; 'wrote:': 0.16; 'example.': 0.18; '(like': 0.21; 'later': 0.21; 'header:In-Reply- To:1': 0.22; 'tue,': 0.23; 'ones.': 0.23; 'received:209.85.213.46': 0.23; 'received:mail- yw0-f46.google.com': 0.23; 'pm,': 0.24; 'testing': 0.24; 'variable': 0.24; 'aug': 0.24; 'code': 0.25; 'fact': 0.27; 'code,': 0.28; 'problem': 0.28; 'theoretical': 0.29; 'message- id:@mail.gmail.com': 0.29; 'operations': 0.30; 'times.': 0.30; 'accidentally': 0.30; 'differently': 0.30; 'subject:?': 0.31; 'error': 0.32; 'version': 0.32; 'list': 0.32; 'source': 0.33; 'it.': 0.33; 'actually': 0.33; 'there': 0.33; 'to:addr:python- list': 0.33; 'instead': 0.33; "i've": 0.34; 'on,': 0.34; 'done': 0.34; 'test': 0.34; 'list.': 0.35; 'rather': 0.35; 'unless': 0.36; 'problem.': 0.36; 'file': 0.36; 'using': 0.37; 'several': 0.37; 'but': 0.37; 'strong': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'extremely': 0.40; 'more': 0.60; 'your': 0.61; 'back': 0.62; 'waiting': 0.63; 'further': 0.64; 'contributors': 0.84; 'deliberate': 0.84; 'dozens': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=8U37Wk38lPonBmxM2lf+9edOxaUFxeSScbV+14QXXIY=; b=nd1ilH9brM30vGppIL8ce+mDEGyyW/tUps3F5tQPMVbtyq4j7DfnFdpG2JQ3sX+MqY ofPkMwES2J9uGUi8Qd83muu2h70tbfdN6ptMPVUtPChCcSiiLbqWjQYgnSY7QXQYf9BE B5aUq1cDvRq+WIhOVOJhZXWUy1/8HJKB6yS0U= MIME-Version: 1.0 In-Reply-To: <2A3EB048-1F10-46E8-973F-9BBD303E7B9F@semanchuk.com> References: <4e49c89a$0$30001$c3e8da3$5496439d@news.astraweb.com> <4e49fcd7$0$29974$c3e8da3$5496439d@news.astraweb.com> <2A3EB048-1F10-46E8-973F-9BBD303E7B9F@semanchuk.com> Date: Tue, 16 Aug 2011 16:12:44 +0100 Subject: Re: Why no warnings when re-assigning builtin names? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313507567 news.xs4all.nl 23963 [2001:888:2000:d::a6]:45151 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11562 On Tue, Aug 16, 2011 at 3:13 PM, Philip Semanchuk wr= ote: > I am an example. I know enough to turn the theoretical warning on, and I = would if I could. I have never shadowed a builtin deliberately. I've done i= t accidentally plenty of times. There are 84 builtins in my version of Pyth= on and I don't have them all memorized. The fact that my editor colors them= differently is the only thing I have to back up my leaky memory. Not all e= ditors are so gracious. > Rather than "turn a warning on" you can "run your code through a linting script". There are several excellent ones. Add it to your makefile or test suite; then you get the testing done over _all_ of your code, instead of waiting until the moment when you actually execute it. > One need look no further than the standard library to see a strong counte= rexample. grep through the Python source for " file =3D". I see dozens of e= xamples of this builtin being used as a common variable name. I would call = contributors to the standard library above-average coders, and we can see t= hem unintentionally shadowing builtins many times. > There are several types of shadowing: 1) Deliberate shadowing because you want to change the behavior of the name. Extremely rare. 2) Shadowing simply by using the name of an unusual builtin (like 'file') in a context where you never use it. Very common. 3) Unintentional shadowing where you create a variable, but then intend to use the builtin. This is the only one that's a problem. list =3D [...] is not a problem unless you later on use foo =3D list(map(...)) which is more common in Python 3 than Python 2, but fortunately, it'll throw a nice quick error - nobody's going to use list operations on the normal 'list' type, nor is anybody going to call an instance of list. Definitely a job for linting. ChrisA