Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'say,': 0.05; 'subject:Python': 0.06; 'assignment': 0.07; 'correct.': 0.07; 'explicit': 0.07; 'apis': 0.09; 'exec': 0.09; 'function,': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'separating': 0.09; 'subject:language': 0.09; 'works.': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'template': 0.14; 'api,': 0.16; 'comp:': 0.16; 'corresponds': 0.16; 'for,': 0.16; 'func': 0.16; 'globals(),': 0.16; 'jumping': 0.16; 'lambda': 0.16; 'notation': 0.16; 'quoted': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'statements,': 0.16; 'subject: \n ': 0.16; 'subject:?)': 0.16; 'subject:unicode': 0.16; 'syntactic': 0.16; 'which,': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'written': 0.21; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'this:': 0.26; 'certain': 0.27; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '(this': 0.29; 'am,': 0.29; "doesn't": 0.30; 'statement': 0.30; "skip:' 10": 0.31; "d'aprano": 0.31; 'implicit': 0.31; 'ordinary': 0.31; 'parameters.': 0.31; 'steven': 0.31; 'maybe': 0.34; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'common': 0.35; 'something': 0.35; 'but': 0.35; 'belong': 0.36; 'marks': 0.36; 'done': 0.36; 'similar': 0.36; 'example,': 0.37; 'two': 0.37; 'list': 0.37; 'clear': 0.37; 'others.': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'quote': 0.39; 'support,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'expression': 0.60; 'received:173': 0.61; 'course': 0.61; 'name': 0.63; 'sum': 0.64; 'more': 0.64; 'within': 0.65; 'answer.': 0.68; 'subject:this': 0.83; 'calls,': 0.84; 'high,': 0.84; 'received:fios.verizon.net': 0.84; 'together,': 0.84; 'either:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Time we switched to unicode? (was Explanation of this Python language feature?) Date: Tue, 25 Mar 2014 19:55:39 -0400 References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <0b78649a-16b3-4410-8258-e859578d62be@googlegroups.com> <281c8ce1-4f03-4e93-b5cd-d45b85e89e7e@googlegroups.com> <53314FC1.2010603@rece.vub.ac.be> <5331652c$0$29994$c3e8da3$5496439d@news.astraweb.com> <53319e60$0$29994$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: <53319e60$0$29994$c3e8da3$5496439d@news.astraweb.com> 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: 102 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395791763 news.xs4all.nl 2877 [2001:888:2000:d::a6]:54410 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69082 On 3/25/2014 11:18 AM, Steven D'Aprano wrote: > The thing is, we can't just create a =E2=88=91 function, because it doe= sn't work > the way the summation operator works. The problem is that we would want= > syntactic support, so we could write something like this: > > p =3D 2 > =E2=88=91(n, 1, 10, n**p) Of course we can. If we do not insist on separating the dummy name from=20 the expression that contains it. this works. def sigma(low, high, func): sum =3D 0 for i in range(low, high+1): sum +=3D func(i) return sum p =3D 2 print(sigma(1, 10, lambda n: n**p)) >>> 385 which looks correct. This corresponds to a notation like so 10 =E2=88=91 n: n**p 1 which, for ranges, is more sensible that the standard. Lambda is an=20 explicit rather than implicit quotation. If you do insist on separating two things that belong together, you can=20 quote with quote marks instead and join within the function to get the=20 same answer. def sig2(var, low, high, exp): func =3D eval("lambda {}: {}".format(var, exp)) sum =3D 0 for i in range(low, high+1): sum +=3D func(i) return sum p =3D 2 print(sig2('n', 1, 10, 'n**p')) >>> 385 To me, both these apis are 'something' like the api with implicit=20 quoting, which is impossible for function calls, but common in=20 statements. (This is one reason to make a construct a python statement=20 rather than function. Jumping is another) Using the same api, one could=20 instead expand the template to include the for loop and use exec instead = of eval. def sig3(var, low, high, exp): loca =3D {} exec( '''\ sum =3D 0 for {} in range({}, {}): sum +=3D {}\ '''.format(var, low, high+1, exp), globals(), loca) return loca['sum'] print(sig3('n', 1, 10, 'n**p')) > This cannot be an ordinary function, because if it were, the n**p > expression would be evaluated before passing the result to the function= =2E So would the dummy parameter name n. > We want it to delay evaluation, like a list comp: > > [n**p for n in range(1, 11)] > > the expression n**p gets evaluated inside the list comp. Which is to say, it is implicitly quoted > That cannot be written as a function either: > > list_comp(n**p, n, range(1, 11) It can be with explicit quoting, similar to how done for sigma. Some=20 lisps once and maybe still have 'special' functions that implicitly=20 quote certain arguments. One just has to learn which functions and which = parameters. I prefer having implicit quoting relegated to a limited set=20 of statements, where is it pretty clear what must be quoted for the=20 statement to make any sense. Assignment is one example, while, for, and=20 comprehensions are others. --=20 Terry Jan Reedy