Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'operator': 0.03; 'argument': 0.05; 'subject:Python': 0.06; 'extent': 0.07; 'constructor': 0.09; 'oh,': 0.09; 'python': 0.11; 'formatted': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'worse.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'code,': 0.22; 'char': 0.24; 'string,': 0.24; '(or': 0.24; 'sort': 0.25; 'this:': 0.26; 'header :In-Reply-To:1': 0.27; 'function': 0.29; '[1]': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'easy,': 0.31; 'exceptions': 0.31; 'struct': 0.31; 'url:se': 0.31; 'class': 0.32; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'c++': 0.36; 'functions.': 0.36; 'returning': 0.36; 'sometimes': 0.38; 'problems': 0.38; 'checks': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'even': 0.60; 'most': 0.60; "you'll": 0.62; 'making': 0.63; 'more': 0.64; 'business': 0.70; 'safe.': 0.84; '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=g1d3lxXat+TGpxRkjVa8N8k2qVcCLWIAqUNuRKemQys=; b=BYM3DDcAXqYJO5qxTHAn7FJiCfm9BhX6fJEC0456H4fqdY2K0KANrHmAFkEE+IGL9W OfqEZBNZQ9Mj/FOm2pzxaAosCjUaGZkz7fQ2o/Ys4edq6jrQ9nfzZZfKTlSUmCWh2h/q avcHis4jk/gNEeZLEe+ETMazZpErlp48aDqn+hXIKxVAF4YWQD8oFGRgo6cVvqduYPD1 d56ZgKpKaC4tsJIOnq98NWTWhGBAljXM7irpJtOCRXf2iFJLtUK6xxLeCxe5k9785S1S OxC1TP8TaqIh9os+W+03FybN0IJqHlF229aRrIV50Gt/DgzkG+Uy26+QtX+WSqrLWGI1 bT6w== MIME-Version: 1.0 X-Received: by 10.66.121.234 with SMTP id ln10mr5444709pab.20.1383347606860; Fri, 01 Nov 2013 16:13:26 -0700 (PDT) In-Reply-To: <107ac59a-229c-4da0-bea1-a93834624bc8@googlegroups.com> References: <107ac59a-229c-4da0-bea1-a93834624bc8@googlegroups.com> Date: Sat, 2 Nov 2013 10:13:26 +1100 Subject: Re: Python wart 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383347617 news.xs4all.nl 15963 [2001:888:2000:d::a6]:49997 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58298 On Sat, Nov 2, 2013 at 8:47 AM, Peter Cacioppi wrote: > I always thought printf was sort of crappy. > > I thought the C++ << business even worse. Oh, you'll get no argument from me about the std::*stream types! When I write C++ code, I almost exclusively use C-style formatted strings, even sometimes going to the extent of using fopen() just so I can use fprintf() rather than fstream. Also, I often create a class something like this: struct format { char *data; format(const char *fmt, ...); operator char *() {return data;} }; where the constructor calls vsprintf and there's a bit of new/delete work to manage memory, but that's all under the covers; in code, I use it like this: some_function_call(format("Hello, %s!", "world")); making it almost as if it's a function returning a string, like you would in Python. Most of the problems with printf come from the fragility of C's variadic functions. Some C compilers can deal with that (gcc happily checks printf args), but there are still fundamentally hard problems around it. They don't apply in Python (or Pike, which has an sprintf function[1] with even more power), as issues can be signalled with exceptions - clean, easy, safe. ChrisA [1] http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/sprintf.html