Rule of order for dot operators?

Cameron Simpson cs at zip.com.au
Mon May 18 18:29:50 EDT 2015


On 16May2015 12:20, C.D. Reimer <chris at cdreimer.com> wrote:
>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.
>
>Does python perform the dot operators from left to right or according
>to a rule of order (i.e., multiplication/division before add/subtract)?

I've been thinking about the mindset that asks this question.

I think the reason you needed to ask it was that you were thinking in terms of 
each .foo() operation acting on the original "slug". They do not.

"slug" is an expression returning, of course, itself.

"slug.title()" is an expression returning a new string which is a titlecased 
version of "slug".

"slug.title().replace(...)" is an expression which _operates on_ that new 
string, _not_ on "slug".

In particular, each .foo() need not return a string - it might return anything, 
and the following .bah() will work on that anything.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list