Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python Subject: Re: Method Chaining Date: Sun, 19 Jun 2016 08:56:44 -0700 Lines: 45 Message-ID: References: <74831a84-7b05-4242-b4c0-4a90d147717b@googlegroups.com> <33f73a24-4dcf-429b-9264-a3f3098f1cc3@googlegroups.com> <5763c2a7$0$1602$c3e8da3$5496439d@news.astraweb.com> <2dffea93-3dbd-4d04-93ed-6e02e3dd8660@googlegroups.com> <5765B36F.5010506@stoneleaf.us> <5766B3E1.2020807@stoneleaf.us> <5766C0BC.1070009@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 7qqxkSFXMzbtr4SwND2+zgisr8Jo/T4fonky+lismPdA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attributes': 0.07; 'modifying': 0.07; 'omit': 0.07; 'ambiguity': 0.09; 'attribute.': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'obj,': 0.09; 'raised.': 0.09; 'question.': 0.13; '2016': 0.16; 'innermost': 0.16; 'joonas': 0.16; 'lend': 0.16; 'liik': 0.16; 'nightmare': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'attribute': 0.18; 'nested': 0.18; 'say,': 0.18; '>>>': 0.20; 'otherwise,': 0.20; 'object.': 0.22; 'am,': 0.23; "python's": 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'earlier': 0.27; 'fine': 0.28; 'now?': 0.29; 'skip:. 10': 0.32; 'michael': 0.33; "skip:' 20": 0.34; 'structure': 0.34; 'could': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'say': 0.37; 'itself': 0.38; 'anything': 0.38; 'why': 0.39; 'sure': 0.39; 'does': 0.39; 'from:': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'leading': 0.61; 'charset:windows-1252': 0.62; 'more': 0.63; '>>>>>': 0.66; "'with'": 0.84; 'does?': 0.84; 'ethan': 0.91; 'furman': 0.91 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5766C0BC.1070009@stoneleaf.us> X-Mailman-Original-References: <74831a84-7b05-4242-b4c0-4a90d147717b@googlegroups.com> <33f73a24-4dcf-429b-9264-a3f3098f1cc3@googlegroups.com> <5763c2a7$0$1602$c3e8da3$5496439d@news.astraweb.com> <2dffea93-3dbd-4d04-93ed-6e02e3dd8660@googlegroups.com> <5765B36F.5010506@stoneleaf.us> <5766B3E1.2020807@stoneleaf.us> Xref: csiph.com comp.lang.python:110145 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