Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7122
| References | <BANLkTi=VUwFDv5otq=7CMngbWzHy3Fo50A@mail.gmail.com> <mailman.2491.1307377282.9059.python-list@python.org> <87zklu1ox6.fsf@benfinney.id.au> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2011-06-06 18:38 -0600 |
| Subject | Re: new string formatting with local variables |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2512.1307407121.9059.python-list@python.org> (permalink) |
On Mon, Jun 6, 2011 at 6:11 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> Chris Rebert <clp2@rebertia.com> writes:
>
>> print "{solo} was captured by {jabba}".format(**locals()) # RIGHT
>
> I tend to use ‘u"foo {bar} baz".format(**vars())’, since ‘vars’ can also
> take the namespace of an object. I only need to remember one “give me
> the namespace” function for formatting.
If you're using an object namespace, then you can just do this:
print("{o.solo} was captured by {o.jabba}".format(o=self))
That looks a lot cleaner to me than passing in **vars(self). For
locals(), I can see the appeal, but I tend to avoid it because it has
the same icky feeling as doing an import *.
Cheers,
Ian
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: new string formatting with local variables Chris Rebert <clp2@rebertia.com> - 2011-06-06 09:21 -0700
Re: new string formatting with local variables Ben Finney <ben+python@benfinney.id.au> - 2011-06-07 10:11 +1000
Re: new string formatting with local variables Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 18:31 -0600
Re: new string formatting with local variables Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 18:38 -0600
Re: new string formatting with local variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-07 03:57 +0000
Re: new string formatting with local variables Ben Finney <ben+python@benfinney.id.au> - 2011-06-07 17:47 +1000
csiph-web