Movatterモバイル変換
[0]ホーム
[Python-ideas] Briefer string format
Eric V. Smitheric at trueblade.com
Mon Jul 20 19:08:37 CEST 2015
On 07/20/2015 10:19 AM, Eric V. Smith wrote:> On 07/20/2015 10:08 AM, Ryan Gonzalez wrote:>> I would prefer something more like:>>>> def f(s):>> caller = inspect.stack()[1][0]>> return s.format(dict(caller.f_globals, **caller.f_locals))>> You need to use format_map (or **dict(...)). And ChainMap might be a> better choice, it would take some benchmarking to know.>> Also, you don't get builtins using this approach. I'm using eval to> exactly match what evaluating the variable in the parent context would> give you. That might not matter depending on the actual requirements.>> But I agree there are multiple ways to do this, and several of them> could be made to work. Mine might have fatal flaws that more testing> would show.My quick testing comes up with this, largely based on the code by joejev:import sysimport collectionsdef f(str): frame = sys._getframe(1) return str.format_map(collections.ChainMap( frame.f_locals, frame.f_globals, frame.f_globals['__builtins__'].__dict__))I'm not sure about the builtins, but this seems to work. Also, you mightwant to be able to pass in the frame depth to allow this to be callablemore than 1 level deep.So, given that this is all basically possible to implement today (at thecost of using sys._getframe()), I'm -1 on adding any compiler tricks tosupport this via syntax. From what I know of PyPy, this should besupported there, albeit at a large performance cost.Eric.
More information about the Python-ideasmailing list
[8]ページ先頭