Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ervin =?utf-8?Q?Heged=C3=BCs?= Newsgroups: comp.lang.python Subject: Re: String format - resolve placeholders names Date: Fri, 20 Nov 2015 16:53:00 +0100 Lines: 37 Message-ID: References: <20151120145227.GB13994@arxnet.hu> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de zLaPStui2nhs4v7zyUTE6wB/G/xQGatKO3GlzLGUw8Mg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '21,': 0.07; 'cc:addr :python-list': 0.09; 'python': 0.10; 'def': 0.13; 'chris,': 0.16; 'help?': 0.16; 'key):': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:String': 0.16; 'wrote:': 0.16; 'string': 0.17; 'exists': 0.18; 'skip:{ 20': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'am,': 0.23; 'sat,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'skip:_ 10': 0.32; 'class': 0.33; 'received:google.com': 0.35; 'done': 0.35; 'mapping': 0.35; 'nov': 0.35; 'received:74.125.82': 0.35; 'asking': 0.35; 'but': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'does': 0.39; '(direct)': 0.84; 'again!': 0.84; 'object:': 0.84; 'received:86': 0.93; 'received:hu': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=ptJTOihvPFaPFQ2XYU+TD4XRFpDpJAOJiPSdVmhR1co=; b=BYE/IVHVIXRAja2WwGb/gZMTQu8stkzZS2TzEIw4DH/QuWV0KpUA0Id7mOWBuZWi+U eg35vt5pnCw2GojzNwpt4TeGRTFiywTsiPLQzexlphXj+S69Ygs3x7/XzEc6WWUin6SN rtF6Bf4DEb08BoPLvkcrRQRzPjC/TYT8qJ9wPuo6aMef1eQ62sdIQUEE88AVcrFTxl28 FagtIquUB2+pgq3x+3mflj8ccTxbTpAawpLKFVisKjTWnhPsaDw9JMOWGGSs5yjbDehH jpI4UsyDuoN65WMXlwFmvCzYarO0Djp0kaqgwP8iISpAkA8xmBX75qxX9GQZWaSU5hzz CvKA== X-Received: by 10.28.19.20 with SMTP id 20mr684996wmt.49.1448034769710; Fri, 20 Nov 2015 07:52:49 -0800 (PST) Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99157 Hello Chris, On Sat, Nov 21, 2015 at 02:06:11AM +1100, Chris Angelico wrote: > On Sat, Nov 21, 2015 at 1:52 AM, Ervin Hegedüs wrote: > > Python has a good string formatter, eg. I can do this: > > > > s = "{who} likes {what}" > > d = {'who': "Adam", 'what': "ants"} > > s.format(**d) ... > > But is there any other (direct) way, which keywords exists in > > string? > > I think what you're asking for can be done using format_map with a > custom mapping object: > > >>> class IdentiMap: > ... def __init__(self): > ... self.keys = [] > ... def __getitem__(self, key): > ... self.keys.append(key) > ... > >>> m = IdentiMap() > >>> "{who} likes {what}".format_map(m) > 'None likes None' > >>> m.keys > ['who', 'what'] > > Does that help? absolutely, many thanks again! a.