Method Chaining

Ethan Furman ethan at stoneleaf.us
Sun Jun 19 11:56:44 EDT 2016


On 06/19/2016 08:14 AM, Michael Torrie wrote:
> 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.

Sure it would, just like any 'this_thing.whatever = 9' works just fine 
if 'this_thing' doesn't already a `whatever` attribute.

The only thing that would change is being able to omit the 'this_thing' 
portion; if you want to access an earlier 'with' obj, then you must be 
explicit:

   with ob_a:
      with ob_b:
         ob_a.whatever = 9
         .something_else = 10

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

This is no more ambiguous than any other nested structure and, in some 
cases, even simpler.

--
~Ethan




More information about the Python-list mailing list