Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90818 > unrolled thread

Re: Rule of order for dot operators?

Started byCameron Simpson <cs@zip.com.au>
First post2015-05-19 08:29 +1000
Last post2015-06-08 23:06 +1000
Articles 7 — 6 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.


Contents

  Re: Rule of order for dot operators? Cameron Simpson <cs@zip.com.au> - 2015-05-19 08:29 +1000
    Re: Rule of order for dot operators? Rustom Mody <rustompmody@gmail.com> - 2015-05-18 18:32 -0700
      Re: Rule of order for dot operators? Ron Adam <ron3200@gmail.com> - 2015-05-18 22:43 -0400
      Re: Rule of order for dot operators? Chris Angelico <rosuav@gmail.com> - 2015-05-19 16:25 +1000
      Re: Rule of order for dot operators? Ron Adam <ron3200@gmail.com> - 2015-05-19 14:02 -0400
    Re: Rule of order for dot operators? albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-06-08 11:21 +0000
      Re: Rule of order for dot operators? Steven D'Aprano <steve@pearwood.info> - 2015-06-08 23:06 +1000

#90818 — Re: Rule of order for dot operators?

FromCameron Simpson <cs@zip.com.au>
Date2015-05-19 08:29 +1000
SubjectRe: Rule of order for dot operators?
Message-ID<mailman.118.1431989304.17265.python-list@python.org>
On 16May2015 12:20, C.D. Reimer <chris@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@zip.com.au>

[toc] | [next] | [standalone]


#90821

FromRustom Mody <rustompmody@gmail.com>
Date2015-05-18 18:32 -0700
Message-ID<fabab250-d7a5-49f4-a9ef-359bd003a607@googlegroups.com>
In reply to#90818
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!!

[toc] | [prev] | [next] | [standalone]


#90823

FromRon Adam <ron3200@gmail.com>
Date2015-05-18 22:43 -0400
Message-ID<mailman.120.1432003441.17265.python-list@python.org>
In reply to#90821

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




[toc] | [prev] | [next] | [standalone]


#90827

FromChris Angelico <rosuav@gmail.com>
Date2015-05-19 16:25 +1000
Message-ID<mailman.121.1432016739.17265.python-list@python.org>
In reply to#90821
On Tue, May 19, 2015 at 12:43 PM, Ron Adam <ron3200@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

[toc] | [prev] | [next] | [standalone]


#90884

FromRon Adam <ron3200@gmail.com>
Date2015-05-19 14:02 -0400
Message-ID<mailman.150.1432058593.17265.python-list@python.org>
In reply to#90821

On 05/19/2015 02:25 AM, Chris Angelico wrote:
> On Tue, May 19, 2015 at 12:43 PM, Ron Adam<ron3200@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





[toc] | [prev] | [next] | [standalone]


#92336

Fromalbert@spenarnc.xs4all.nl (Albert van der Horst)
Date2015-06-08 11:21 +0000
Message-ID<55757ad2$0$3063$e4fe514c@dreader35.news.xs4all.nl>
In reply to#90818
In article <mailman.118.1431989304.17265.python-list@python.org>,
Cameron Simpson  <cs@zip.com.au> wrote:
>On 16May2015 12:20, C.D. Reimer <chris@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@zip.com.au>
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#92338

FromSteven D'Aprano <steve@pearwood.info>
Date2015-06-08 23:06 +1000
Message-ID<55759373$0$13010$c3e8da3$5496439d@news.astraweb.com>
In reply to#92336
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web