Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #22416 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2012-03-31 06:29 -0400 |
| Last post | 2012-03-31 10:51 -0700 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: string interpolation for python Terry Reedy <tjreedy@udel.edu> - 2012-03-31 06:29 -0400
Re: string interpolation for python Steve Howell <showell30@yahoo.com> - 2012-03-31 10:51 -0700
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-03-31 06:29 -0400 |
| Subject | Re: string interpolation for python |
| Message-ID | <mailman.1177.1333189777.3037.python-list@python.org> |
On 3/31/2012 2:22 AM, Yingjie Lan wrote:
> Hi all,
>
> I'd really like to share this idea of string interpolation for formatting.
> Let's start with some code:
>
> >>> name = "Shrek"
> >>> print( "Hi, $name$!")
> Hi, Shrek!
> >>> balls = 30
> >>> print( "We have $balls$ balls.")
> We have 30 balls
You can already do essentially that without adding a special-case string
formatting method to the general methods we already have.
>>> balls = 5
>>> people = 3
>>> 'The {people} people have {balls} balls.'.format(**locals())
'The 3 people have 5 balls.'
--
Terry Jan Reedy
[toc] | [next] | [standalone]
| From | Steve Howell <showell30@yahoo.com> |
|---|---|
| Date | 2012-03-31 10:51 -0700 |
| Message-ID | <485f1ac0-b63f-4c83-9a99-22494fb2283d@9g2000pbn.googlegroups.com> |
| In reply to | #22416 |
On Mar 31, 3:29 am, Terry Reedy <tjre...@udel.edu> wrote:
> On 3/31/2012 2:22 AM, Yingjie Lan wrote:
>
> > Hi all,
>
> > I'd really like to share this idea of string interpolation for formatting.
> > Let's start with some code:
>
> > >>> name = "Shrek"
> > >>> print( "Hi, $name$!")
> > Hi, Shrek!
> > >>> balls = 30
> > >>> print( "We have $balls$ balls.")
> > We have 30 balls
>
> You can already do essentially that without adding a special-case string
> formatting method to the general methods we already have.
>
> >>> balls = 5
> >>> people = 3
> >>> 'The {people} people have {balls} balls.'.format(**locals())
> 'The 3 people have 5 balls.'
>
I was wondering how much of a performance penalty you pay for using
the **locals() idiom, because I use it myself sometimes.
It turns out there is a slight penalty for "**locals()" vs. explicitly
passing in arguments to format (e.g. ".format(balls=balls,
people=people"), although it's probably negligible in 99.9% of use
cases.
def yo(a):
x = 1
y = 2
z = 3
a = b = c = d = 7
for i in range(10):
# s = "{x} {y} {z}".format(**locals())
s = "{x} {y} {z}".format(x=x, y=y, z=z)
for i in range(10000):
yo(i)
# .150s for **locals()
# .131s for explicit x/y/z
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web