[Python-ideas] Fix the DRY problem (was Re: PEP 501
[Python-ideas] Fix the DRY problem (was Re: PEP 501 - i18n with marked strings)
Eric Fahlgren ericfahlgren at gmail.comTue Aug 11 22:32:03 CEST 2015
- Previous message (by thread): [Python-ideas] Fix the DRY problem (was Re: PEP 501 - i18n with marked strings)
- Next message (by thread): [Python-ideas] Fix the DRY problem (was Re: PEP 501 - i18n with marked strings)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: Barry Warsaw [mailto:barry at python.org] > But the DRY argument is much more compelling to me, and currently I think the > best way to reduce repetition in function arguments is through sys._getframe() > and other such nasty tricks. I'd really much prefer to see this small annoyance > fixed in a targeted way than add a hugely complicated new feature that reduces > readability (IMHO). > > Which is why I like the scope() and similar ideas. Something like a built-in that This +100. I've been doing my own variants on the f-string for over a decade and every time I type "sys.__getframe()" I think to myself, "There's got to be a better way..." Here's a stripped-down example from Real Life (gah!): >>> def formatSolverFunction(self, s, **kwds): >>> localFrame = sys._getframe().f_back >>> localDict = localFrame.f_locals >>> localDict.update(kwds) # Our **kwds override locals. >>> globalDict = localFrame.f_globals >>> >>> #----------------------------------------------------------------------- >>> def sub(match): >>> fmt = match.groups()[0].split(":", 1) >>> val = eval(fmt[0], globalDict, localDict) >>> >>> if isinstance(val, SimulatableObject): >>> val = val.id >>> >>> if len(fmt) > 1: >>> try: >>> val = val.__format__(fmt[1]) >>> except ValueError as e: >>> ... error reporting ... >>> return str(val) >>> >>> while True: >>> s, n = re.subn(r"{([\w.:]*)}", sub, s) # We don't support !s or !r. >>> if n == 0: break >>> return s
- Previous message (by thread): [Python-ideas] Fix the DRY problem (was Re: PEP 501 - i18n with marked strings)
- Next message (by thread): [Python-ideas] Fix the DRY problem (was Re: PEP 501 - i18n with marked strings)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list