Rule of order for dot operators?

Rustom Mody rustompmody at gmail.com
Mon May 18 21:32:11 EDT 2015


On Tuesday, May 19, 2015 at 4:18:36 AM UTC+5:30, Cameron Simpson wrote:
> On 16May2015 12:20, C.D. Reimer  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.

For an arbitrary binary operator ◼
x ◼ y ◼ z
can group as
(x◼y)◼z
or
x◼(y◼z)

One could (conceivably) apply the same rule to x.y.z
Except that x.(y.z) is a bit hard to give a meaning to!!



More information about the Python-list mailing list