On Sep 13, 9:54 pm, Sean DiZazzo <half.ital...@gmail.com> wrote: > > > I have never used a call to "locals()" in my code. Can you show me a > > > use case where it is valuable and Pythonic? > > > def print_item(item): > > description = textwrap.fill(item.description, 40) > > short = item.description.split('\n', 1)[0] > > code = str(item.id).zfill(6) > > print "%(code)s %(short)s\n%(description)s\n" % locals() > > I see the use of that, but according to Zen, "Explicit is better than > implicit." > > > > > Transferring arguments: > > > def foo(some, long, list, of, arguments): > > additional = 5 > > return other(**locals()) > > Why not?: > > def foo(**kwargs): > kwargs["additional"] = 5 > return other(**kwargs) > > > Defining properties: > > > class ColourThing(object): > > �...@apply > > def rgb(): > > def fset(self, rgb): > > self.r, self.g, self.b = rgb > > def fget(self): > > return (self.r, self.g, self.b) > > return property(**locals()) > > So really it's just a short hand. But it's against the Zen! Explicit > not Implicit! I'm not convinced....then again, I didn't look at the > source code of the standard libraries.
ISTM that any use of locals() is going to be labeled "implicit" by you, which is not unfair, since it pretty much is implicit compared to using local variables directly. If being implicit is alone enough to make anything unPythonic, then the answer to your question is No, locals() is never Pythonic. If you are willing to open your mind to the possibility that some Pythonic things don't adhere to every Zen, then I would suggest that Gabrielle's examples are perfectly Pythonic shortcuts. But if you don't want to use them, you don't have to, nobody is forcing you. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list