Rule of order for dot operators?

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

C.D. Reimer
Greetings,

Noobie 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.

Does python perform the dot operators from left to right or according to
a rule of order (i.e., multiplication/division before add/subtract)?

Thank you,

Chris Reimer


Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Gary Herron-2
On 05/16/2015 12:20 PM, C.D. Reimer wrote:

> Greetings,
>
> Noobie 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.
>
> 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, that's correct.

Gary Herron



>
> Thank you,
>
> Chris Reimer


--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418



Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Joel Goldstick-2
In reply to this post by C.D. Reimer
On Sat, May 16, 2015 at 3:20 PM, C.D. Reimer <chris at cdreimer.com> wrote:

> Greetings,
>
> Noobie 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.
>
> Does python perform the dot operators from left to right or according to a
> rule of order (i.e., multiplication/division before add/subtract)?
>
> Thank you,
>
> Chris Reimer
> --
> https://mail.python.org/mailman/listinfo/python-list

>From left to right.  So in your first example it replaces - with
space, then capitalizes each word.
In your second example, it does the caps first, then gets rid of the dashes

--
Joel Goldstick
http://joelgoldstick.com


Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Peter Otten
In reply to this post by C.D. Reimer
C.D. Reimer wrote:

> Greetings,
>
> Noobie 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.
>
> Does python perform the dot operators from left to right or according to
> a rule of order (i.e., multiplication/division before add/subtract)?

You can find out yourself by using operations where the order does matter:

"Test".upper().lower()




Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

TIm Chase-3
In reply to this post by C.D. Reimer
On 2015-05-16 12:20, C.D. Reimer wrote:
> 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, Python evaluates dot-operators from left to right.

-tkc




Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

C.D. Reimer
In reply to this post by Peter Otten
On 5/16/2015 12:34 PM, Peter Otten wrote:
> You can find out yourself by using operations where the order does
> matter: "Test".upper().lower()

I was wondering about that and couldn't think of an example off the top
of my head.

Thank you,

Chris Reimer


Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Thomas 'PointedEars' Lahn
In reply to this post by C.D. Reimer
Tim Chase wrote:

> On 2015-05-16 12:20, C.D. Reimer wrote:
>> 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, Python evaluates dot-operators from left to right.

?.? is _not_ an operator in Python:

<https://docs.python.org/3/reference/lexical_analysis.html#operators>

--
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.


Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Cameron Simpson
In reply to this post by C.D. Reimer
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>


Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Rustom Mody
In reply to this post by C.D. Reimer
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!!


Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Ron Adam-2


On 05/18/2015 09:32 PM, Rustom Mody wrote:

>> >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!!

Yes.

Having just implementing something similar for nested scopes, it turns out
it can't be operators because if it was, then the names y and z would be
resolved in the wrong scope.

          y = "m"
          z = "n"
          a = x . y . z

Which of course wouldn't do what we want.

          a = x . "m" . "n"

And most likely this would give an error.


The name-part after the dot is evaluated in the next inner scope.  y is
resolved in x's scope, and z is resolved in y's scope.

Which is why you can implement objects with closures, but you need to delay
name resolution to do that.   Which is what the "." does.


Pythons attribute lookup is a bit more complex than this of course.

      https://docs.python.org/3.4/howto/descriptor.html


Cheers,
    Ron







Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Chris Angelico
On Tue, May 19, 2015 at 12:43 PM, Ron Adam <ron3200 at gmail.com> wrote:

> Having just implementing something similar for nested scopes, it turns out
> it can't be operators because if it was, then the names y and z would be
> resolved in the wrong scope.
>
>          y = "m"
>          z = "n"
>          a = x . y . z
>
> Which of course wouldn't do what we want.
>
>          a = x . "m" . "n"
>
> And most likely this would give an error.

If you want to implement the dot as an operator, you could do it by
having a special syntactic element called an "atom", which is used for
these kinds of identifier-like tokens. The dot operator could then
take an object and an atom, and effectively return getattr(obj,
stringify(atom)). I'm fairly sure this would result in the same syntax
as Python uses.

ChrisA


Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Ron Adam-2


On 05/19/2015 02:25 AM, Chris Angelico wrote:

> On Tue, May 19, 2015 at 12:43 PM, Ron Adam<ron3200 at gmail.com>  wrote:
>> >Having just implementing something similar for nested scopes, it turns out
>> >it can't be operators because if it was, then the names y and z would be
>> >resolved in the wrong scope.
>> >
>> >          y = "m"
>> >          z = "n"
>> >          a = x . y . z
>> >
>> >Which of course wouldn't do what we want.
>> >
>> >          a = x . "m" . "n"
>> >
>> >And most likely this would give an error.

> If you want to implement the dot as an operator, you could do it by
> having a special syntactic element called an "atom", which is used for
> these kinds of identifier-like tokens. The dot operator could then
> take an object and an atom, and effectively return getattr(obj,
> stringify(atom)). I'm fairly sure this would result in the same syntax
> as Python uses.

I think it's better not to.   What practical things can be done if the dot
was an operator and names after dots where parsed as atoms?

What I did was parse a name to a subtype of tuple with elements of strings.
  [return name.with.dots] becomes this in memory after parsing.

     [keyword_object name_object]

Using dots as operators would make that...

     [keyword_object name_object dot_object atom_object dot_object
      atom_object]

This would require the interpreter to do in steps what a single function
call can do all at once.

Cheers,
    Ron








Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Albert van der Horst
In reply to this post by C.D. Reimer
In article <mailman.118.1431989304.17265.python-list at python.org>,
Cameron Simpson  <cs at zip.com.au> wrote:

>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".

Why is "slug.title" a valid decomposition of the total string>
(Or is it?)
What is the ()-brackets doing? Does it force the execution of title,
which gives something to be dotted onto slug etc. See below.

>
>"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.

I interpreted the question as about the associative of the
dot operator.

title = slug.title().replace('-',' ')

Does that mean
title = slug.( title().replace('-',' ') )
or

title = ( slug.( title()) .replace('-',' ')

or is it even    <slot>.<slot>.<slot> a ternary operator with
special syntax like we have in
    <slot> <= <slot> <= <slot>
lately.

It seems to me that you presuppose the answer.

>
>Cheers,
>Cameron Simpson <cs at zip.com.au>
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst


Reply | Threaded
Open this post in threaded view
|

Rule of order for dot operators?

Steven D'Aprano-8
On Mon, 8 Jun 2015 09:21 pm, Albert van der Horst wrote:


> Why is "slug.title" a valid decomposition of the total string>
> (Or is it?)

I'm afraid I don't understand the question.


> What is the ()-brackets doing? Does it force the execution of title,
> which gives something to be dotted onto slug etc. See below.

slug.title looks up an attribute called "title" attached to the object
called "slug". In this case, slug is a string, so slug.title finds the
string title method:


py> slug = "hello"
py> slug.title
<built-in method title of str object at 0xb7b036e0>


Then the round brackets (parentheses) calls that method with no arguments,
which returns a new string:

py> slug.title()
'Hello'



> I interpreted the question as about the associative of the
> dot operator.

Technically, dot is not an operator. I believe that the docs call it a
delimiter, but in once sense it is more than that because it also performs
an attribute lookup.

In any case, the order of applying dots is *strictly* left-to-right.




--
Steven