Method Chaining

Michael Torrie torriem at gmail.com
Sun Jun 19 11:14:21 EDT 2016


On 06/19/2016 09:01 AM, Ethan Furman wrote:
> On 06/19/2016 04:56 AM, Joonas Liik wrote:
>> On 18 June 2016 at 23:47, Ethan Furman wrote:
>>> On 06/18/2016 07:05 AM, Joonas Liik wrote:
> 
>>>> 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?
>>
>> What if ob_b does not have attribute attr_c but ob_a does?
> 
> Good question.  I would say that _only_ the innermost with object is 
> searched, and if it doesn't have the requested attribute an 
> AttributeError is raised.  Otherwise, as you say, it could be a 
> nightmare to maintain.

But that wouldn't work either because it would make it impossible to
*set* attributes on an object.

If ob_a had attr_c but object ob_b did not, should it set the attribute
on ob_b or overwrite the attribute on ob_a?

Python's dynamic nature just doesn't lend itself to this kind of ambiguity.



More information about the Python-list mailing list