Method Chaining

Joonas Liik liik.joonas at gmail.com
Sun Jun 19 07:56:44 EDT 2016


On 18 June 2016 at 23:47, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 06/18/2016 07:05 AM, Joonas Liik wrote:
>>
>> On 18 June 2016 at 15:04, Pete Forman wrote:
>
>
>>> with obj:
>>>      .a = 1                # equivalent to obj.a = 1
>>>      .total = .total + 1   # obj.total = obj.total + 1
>>
>>
>> the leading dot does not resolve the ambiguity that arises from:
>>
>> with ob_a:
>>      with ob_b:
>>          .attr_c = 42 # which object are we modifying right now?
>
>
> The innermost one.  Why would it be anything else?
>
> --
> ~Ethan~
>
> --
> https://mail.python.org/mailman/listinfo/python-list

What if ob_b does not have attribute attr_c but ob_a does?

This may be simple for a computer to solve - try looking it up on ob_b
and fall back to ob_a if ob_b has no such attr..  but it is hard(er)
to reason about for a human. You need to know about the inner
structure of ob_b. not an issue for the person who writes it at first
(unless 2 months have passed ) but a real pain if you are seeing that
bit of code for the first time or are not intimately familiar with
what ob_b is.

Not unsolvable ofc, but makes it easier for obscure bugs to hide.



More information about the Python-list mailing list