Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90755
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.datemas.de!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: Rule of order for dot operators? |
| Date | Sat, 16 May 2015 21:40:08 +0200 |
| Organization | PointedEars Software (PES) |
| Lines | 45 |
| Message-ID | <3341326.d8VUBGAoep@PointedEars.de> (permalink) |
| References | <mailman.80.1431804386.17265.python-list@python.org> |
| Reply-To | Thomas 'PointedEars' Lahn <usenet@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | solani.org 1431805368 17840 eJwFwQkRADAIAzBLMMozOcCt/iUscQuNTYQHnE7oebkOaDQlYEnrYtudepV5SAxn8wpXMB8SPhE8 (16 May 2015 19:42:48 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Sat, 16 May 2015 19:42:48 +0000 (UTC) |
| User-Agent | KNode/4.14.2 |
| X-User-ID | eJwNx0kBwDAIBEBLJFxBDrDgX0I7v1G2Y+1iaqKrWzEkE3CnEA3WtMxqAhpFpUlzcMWS44Ljz8pL7CtOTPQHaPgWrA== |
| Cancel-Lock | sha1:wgJuGkRCdgugKXS/1fLYtVQqCkA= |
| X-NNTP-Posting-Host | eJwFwQkBwDAIA0BLo5BA5ZTPv4TdQSksN4KGxZ5BC7jpucxvgubVG/q0+tj1OG92bk+luPYPLXQSAg== |
| Xref | csiph.com comp.lang.python:90755 |
Show key headers only | View raw
C.D. Reimer wrote:
^^^^
Who?
> Noobie
What?
> question regarding a single line of code that transforms a URL
> slug ("this-is-a-slug") into a title ("This Is A Slug").
>
> title = slug.replace('-',' ').title()
>
> This line also works if I switched the dot operators around.
>
> title = slug.title().replace('-',' ')
>
> I'm reading the first example as character replacement first and title
> capitalization second, and the second example as title capitalization
> first and character replacement second.
You are reading correctly.
> Does python perform the dot operators from left to right or according to
> a rule of order (i.e., multiplication/division before add/subtract)?
Yes. If you debug the code, which you should have done before posting [1] ,
you will see that
'this-is-a-slug'.title() == 'This-Is-A-Slug'
It follows that in this special case it does not matter if you call .title()
before or after .replace().
However, for greater efficiency, in general you should call .replace() in
such a way that the length of the string it operates on is minimized. For
example, if feasible, always slice *before* .replace().
[1] <http://www.catb.org/~esr/faqs/smart-questions.html>
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Rule of order for dot operators? "C.D. Reimer" <chris@cdreimer.com> - 2015-05-16 12:20 -0700
Re: Rule of order for dot operators? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-16 21:40 +0200
Re: Rule of order for dot operators? "C.D. Reimer" <chris@cdreimer.com> - 2015-05-16 12:55 -0700
Re: Rule of order for dot operators? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-17 19:17 +0200
Re: Rule of order for dot operators? Ned Batchelder <ned@nedbatchelder.com> - 2015-05-17 10:30 -0700
Re: Rule of order for dot operators? Rustom Mody <rustompmody@gmail.com> - 2015-05-17 12:13 -0700
Re: Rule of order for dot operators? "C.D. Reimer" <chris@cdreimer.com> - 2015-05-17 11:35 -0700
Re: Rule of order for dot operators? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-17 11:45 +1000
Re: Rule of order for dot operators? Rustom Mody <rustompmody@gmail.com> - 2015-05-16 22:06 -0700
Re: Rule of order for dot operators? Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-17 15:43 +0000
Re: Rule of order for dot operators? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-20 00:30 +0200
Re: Rule of order for dot operators? Ned Batchelder <ned@nedbatchelder.com> - 2015-05-19 18:36 -0700
Re: Rule of order for dot operators? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-20 04:24 +0200
Re: Rule of order for dot operators? Ned Batchelder <ned@nedbatchelder.com> - 2015-05-19 19:44 -0700
Re: Rule of order for dot operators? Chris Angelico <rosuav@gmail.com> - 2015-05-20 13:11 +1000
Re: Rule of order for dot operators? Ben Finney <ben+python@benfinney.id.au> - 2015-05-20 17:29 +1000
Re: Rule of order for dot operators? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-20 12:31 +1000
Re: Rule of order for dot operators? "C.D. Reimer" <chris@cdreimer.com> - 2015-05-17 10:50 -0700
Re: Rule of order for dot operators? Chris Angelico <rosuav@gmail.com> - 2015-05-18 17:40 +1000
Re: Rule of order for dot operators? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-17 11:31 +1000
Re: Rule of order for dot operators? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2015-05-18 14:00 +0200
csiph-web