Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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.037 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'else:': 0.03; 'debug': 0.07; 'oh,': 0.09; 'received:mail-vc0-f174.google.com': 0.09; 'python': 0.11; 'def': 0.12; 'language,': 0.12; 'code?': 0.16; 'fiddle': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'imported.': 0.16; 'recently.': 0.16; 'all.': 0.16; 'language': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'module': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'subject:problem': 0.24; 'mon,': 0.24; 'source': 0.25; 'possibly': 0.26; 'second': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'that.': 0.31; 'comparison': 0.31; 'constant': 0.31; 'received:209.85.220.174': 0.31; 'file': 0.32; 'probably': 0.32; 'another': 0.32; 'call.': 0.33; 'copying': 0.34; 'there,': 0.34; 'maybe': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'could': 0.34; 'subject:with': 0.35; 'received:209.85': 0.35; 'common': 0.35; 'received:209.85.220': 0.35; 'something': 0.35; 'johnson': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'really': 0.36; 'should': 0.36; 'half': 0.37; 'received:209': 0.37; 'being': 0.38; 'improving': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'even': 0.60; 'easy': 0.60; 'cost.': 0.60; 'solve': 0.60; 'subject:"': 0.60; 'skip:t 30': 0.61; 'new': 0.61; 'you.': 0.62; 'subject:The': 0.64; 'more': 0.64; 'containing': 0.69; 'million': 0.74; 'hey,': 0.75; 'everything,': 0.84; 'everywhere.': 0.84; 'pasting': 0.84; 'slowed': 0.84; 'numerous': 0.93; 'rick': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=iBtelj9EtEfwZc/UQcoyOdVbXZmPDWuSulnUuAAeHOw=; b=rBqP4tTM8q+7iu/jmNnpHzBSXi9rDGwE+tKupCDfSGYk+Uz40yAaVCyUgobHHfbPHq GT85R9K44cILhC1uipb21R6EqyNhFwIqHtFd93/FAVvN7cq7bfqlvHPEk1w/FUTKM3A9 7eZoiuFmlnLJvYPwb67s2nPVHQT5jcIhD/y0Wltw+eiRBV6ccRuHLu5MaGAfF1WCwiIY dEjhRPtq7q5e3OzrKZezUhJd34sxuHf76XyBJPfvIZlhoZZbJJ2W+cniE+kI7E5YrFc6 dtjclSW1OMsmxov6SMHmtXEoIOlCwBGUE+HowCl/RMLffx0HO9TLncYMQITBlyUbDIyJ 75CA== MIME-Version: 1.0 X-Received: by 10.52.164.163 with SMTP id yr3mr13296712vdb.76.1370193653044; Sun, 02 Jun 2013 10:20:53 -0700 (PDT) In-Reply-To: <687dea63-84da-4c45-9366-cb5a10665d1f@googlegroups.com> References: <687dea63-84da-4c45-9366-cb5a10665d1f@googlegroups.com> Date: Mon, 3 Jun 2013 03:20:52 +1000 Subject: Re: PyWart: The problem with "print" From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370193662 news.xs4all.nl 15894 [2001:888:2000:d::a6]:50446 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46711 On Mon, Jun 3, 2013 at 3:04 AM, Rick Johnson wrote: > * Woefully inadequate because: Switching on or off the debug > messages is only valid in the current module that the > function was imported. What if you want to kill all > debugprint messages EVERYWHERE? Do you really want to edit > "debug = BOOLEAN" in every source file OR do something > stupid like import debugprint and edit the DEBUG constant > OR even dumber, edit the debugprint source code? GAWD NO! Easy fix to this one. Instead of copying and pasting debugprint into everything, have it in a module and import it everywhere. Then the debug flag will be common to them all. Oh, and you probably want to add **kwargs to debugprint, because the print function does a lot more than sys.stdout.write does: >>> print(1,2,3,4,sep='#') 1#2#3#4 > * But even if you are willing to cope with all the "switch- > on-and-off" nonsense, are you willing to have you code > slowed by numerous calls to a dead function containing a > comparison that will always be false? Hmm. Could be costly. Hey, you know, Python has something for testing that. >>> timeit.timeit('debugprint("asdf")','def debugprint(*args):\n\tif not DEBUG: return\n\tprint(*args)\nDEBUG=False',number=1000000) 0.5838018519113444 That's roughly half a second for a million calls to debugprint(). That's a 580ns cost per call. Rather than fiddle with the language, I'd rather just take this cost. Oh, and there's another way, too: If you make the DEBUG flag have effect only on startup, you could write your module thus: if DEBUG: debugprint=print else: def debugprint(*args,**kwargs): pass So you can eliminate part of the cost there, if it matters to you. If a half-microsecond cost is going to be significant to you, you probably should be looking at improving other areas, maybe using ctypes/cython, or possibly playing with the new preprocessor tricks that have been being discussed recently. There's really no need to change the language to solve one specific instance of this "problem". ChrisA