Method Chaining

Pete Forman petef4+usenet at gmail.com
Sun Jun 19 06:16:45 EDT 2016


Rustom Mody <rustompmody at gmail.com> writes:

> On Saturday, June 18, 2016 at 5:34:30 PM UTC+5:30, Pete Forman wrote:
>> Rustom Mody  writes:
>> [snip]
>>
>> 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

Why stop there? Another long standing bugbear can be addressed with
this.

class Foo:
    def .set(.a, b)  # equivalent to def set(self, a): self.a = a
        .c += b      # equivalent to self.c += b

Explicitly, that is three new syntactic items.

1) . before a method name elides the first argument of self
2) . before an argument is shorthand for assign to a member
3) . before a variable in a method body replaces self.

Earlier I had proposed a new use of "with" that would have changed the
binding in (3) from self to something else. That was not a good idea for
two reasons. Python already provides a reasonable way to achieve that,
as described in the design FAQ. The second reason is that it subverts
the current "with" statement which mandates its with_item to be a
context expression. "as target" is optional and so syntactic
discrimination is not possible.

-- 
Pete Forman



More information about the Python-list mailing list