Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail 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; 'example:': 0.03; 'importing': 0.05; 'ambiguity': 0.09; 'instance.': 0.09; 'subject:Function': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '[1].': 0.16; 'base64': 0.16; 'bit.': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'merit,': 0.16; 'partly': 0.16; 'readability': 0.16; 'ssh': 0.16; 'syntactic': 0.16; 'tends': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'library': 0.18; 'normally': 0.19; 'skip:1 30': 0.19; 'skip:f 30': 0.19; 'slightly': 0.19; 'code,': 0.22; 'example': 0.22; 'programming': 0.22; 'import': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'flows': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'this:': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; '[1]': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; 'easier': 0.31; 'url:se': 0.31; 'figure': 0.32; 'run': 0.32; 'computer.': 0.33; 'minimal': 0.33; 'could': 0.34; 'problem': 0.35; 'subject: (': 0.35; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'should': 0.36; 'half': 0.37; 'sometimes': 0.38; 'does': 0.39; 'flow': 0.39; 'even': 0.60; 'read': 0.60; 'solve': 0.60; 'become': 0.64; 'different': 0.65; 'here': 0.66; 'prompt': 0.68; 'actually,': 0.84; 'sometimes.': 0.84; 'notion': 0.91; 'url:image': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=th6OS0TSkXfOtS+SFYZ2r1NdkFAnJkatxgzAGXNEp5Q=; b=QQr0EYN+elgjo9Ev4MrFYVgim9LWHd6L3ZLuYnW0VNyUZmriZVynI+uYtjwUL5nvKA 7GXbaXGq/r7e0xsUDhf79Wsm483iiPu/NaDe3MZP9XR41FtDMB3jqhuK+27OR+wUYjNH 2DX9S4IvspMv0X8YZC3AN6yAVwI3GbG8yuqcoKGdmnZD3qyBSD9AhBzNWXYwJwVH7gs0 PrMt6D6ChqcPD5l/ZHja0RnZM26eA+j2TJV6E2v/i1TMjUSQBwb7ThxSCHQZ38ajTaGo J5Kx7yXBbAv2VEOKM4uhuYhRI0ChOO9HDX70Jqv2eHwMUDf0HqxlebmhrIx6ss9guL9e /Y1w== MIME-Version: 1.0 X-Received: by 10.52.244.84 with SMTP id xe20mr17118207vdc.3.1402247403691; Sun, 08 Jun 2014 10:10:03 -0700 (PDT) In-Reply-To: <12d31393-3598-4304-8ce9-ac847ac21c64@googlegroups.com> References: <8b96ae27-20fa-4df9-807e-c806fed983c0@googlegroups.com> <38058e64-0113-457c-ae63-cc66e8b569cd@googlegroups.com> <12d31393-3598-4304-8ce9-ac847ac21c64@googlegroups.com> Date: Mon, 9 Jun 2014 03:10:03 +1000 Subject: Re: Uniform Function Call Syntax (UFCS) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402247406 news.xs4all.nl 2918 [2001:888:2000:d::a6]:49990 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72978 On Mon, Jun 9, 2014 at 2:24 AM, jongiddy wrote: > A contrived example - which of these is easier to understand? > > from base64 import b64encode > > # works now > print(b64encode(str(min(map(int, f.readlines()), key=lambda n: n % 10)), b'?-')) > > # would work with UFCS > f.readlines().map(int).min(key=lambda n: n % 10).str().b64encode(b'?-').print() > > You can read the second form left to right Actually, this is something that I've run into sometimes. I can't think of any Python examples, partly because Python tends to avoid unnecessary method chaining, but the notion of "data flow" is a very clean one - look at shell piping, for instance. Only slightly contrived example: cat foo*.txt | gzip | ssh other_server 'gunzip | foo_analyze' The data flows from left to right, even though part of the data flow is on a different computer. A programming example might come from Pike's image library [1]. This definitely isn't what you'd normally call good code, but sometimes I'm working at the interactive prompt and I do something as a one-liner. It might look like this: Stdio.write_file("foo.png",Image.PNG.encode(Image.JPEG.decode(Stdio.read_file("foo.jpg")).autocrop().rotate(0.5).grey())); With UFCS, that could become perfect data flow: read_file("foo.jpg").JPEG_decode().autocrop().rotate(0.5).grey().PNG_encode().write_file("foo.png"); I had to solve the syntactic ambiguity here by importing all the appropriate names, which does damage readability a bit. But you should be able to figure out what this is doing, with only minimal glancing at the docs (eg to find out that rotate(0.5) is rotating by half a degree). So the proposal does have some merit, in terms of final syntactic readability gain. The problem is the internal ambiguity along the way. ChrisA [1] http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/Image/Image.html