Method Chaining

Rustom Mody rustompmody at gmail.com
Sat Jun 18 11:35:57 EDT 2016


On Saturday, June 18, 2016 at 5:34:30 PM UTC+5:30, Pete Forman wrote:
> Rustom Mody  writes:
> 
> > On Friday, June 17, 2016 at 2:58:19 PM UTC+5:30, Steven D'Aprano wrote:
> >> On Fri, 17 Jun 2016 06:13 pm, Ned Batchelder wrote:
> >>
> >> > To me, it's a toss-up. The chained version is nice in that it
> >> > removes the repetition of "g". But the unchained version is more
> >> > explicit, and avoids the awkward parenthesis.
> >> >
> >> > I think I would lean toward the unchained version. Clearly tastes
> >> > can differ.
> >>
> >> Indeed. For what it's worth, I'm ever-so-slightly leaning towards
> >> Lawrence's taste here.
> >
> > More than 'slightly' out here!
> > One thing about python OOP that irritates me is the 'self.' clutter.
> > With a Pascal/VB style with-statement its naturally taken care of
> >
> > Yeah I know there is this FAQ:
> > https://docs.python.org/2/faq/design.html#why-doesn-t-python-have-a-with-statement-for-attribute-assignments
> >
> > I consider it bogus if we allow with to mean something like:
> > https://msdn.microsoft.com/en-us/library/wc500chb.aspx
> 
> One subtle difference between your two citations is that VB uses a
> leading dot. Might that lessening of ambiguity enable a future Python to
> allow this?
> 
> class Foo:
>     def .set(a):  # equivalent to def set(self, a):
>         .a = a    # equivalent to self.a = a
> 

Chuckle! Heck Why not?!
But no I did not think of this

> Unless it is in a with statement
> 
> with obj:
>     .a = 1                # equivalent to obj.a = 1
>     .total = .total + 1   # obj.total = obj.total + 1


I intended only this much viz that the syntactic marker '.' disambiguates
between ordinary variable and attribute

As for what happens when there are nested references as mentioned by Waffle:
Well these ambiguities have a venerable vintage in programming languages
Starting from the dangling else to which declaration a variable refers to
when there are nested scopes and much else ie "match-to-the-innermost" seems
to be obvious enough


More information about the Python-list mailing list