Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Well, I finally ran into a Python Unicode problem, sort of Date: Sun, 3 Jul 2016 17:41:59 +1000 Lines: 56 Message-ID: References: <0589521b-648a-47c1-8afa-6f746e0fb901@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de gkzgbiYFmKgnvWpXmXp04QgrA8FQvqN+J+/K4YybsnOQ== 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; 'subject:Python': 0.05; 'cpython': 0.05; 'defines': 0.07; 'cc:addr:python-list': 0.09; 'derived': 0.09; 'identifier': 0.09; 'identifier,': 0.09; 'inclusion': 0.09; 'non-ascii': 0.09; 'subject:into': 0.09; 'throws': 0.09; 'python': 0.10; 'question.': 0.13; 'interpreter': 0.15; 'weird': 0.15; "(it's": 0.16; '2016': 0.16; 'alphabet': 0.16; 'constitutes': 0.16; 'devs': 0.16; 'formula': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'identifiers,': 0.16; 'naming': 0.16; 'obviously,': 0.16; 'operators.': 0.16; 'permits': 0.16; 'properties,': 0.16; 'python- based': 0.16; 'range,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Unicode': 0.16; 'sure.': 0.16; 'wrote:': 0.16; 'refers': 0.18; 'hack': 0.18; 'variable': 0.18; 'language': 0.19; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'saying': 0.22; "aren't": 0.22; 'subject:problem': 0.22; 'defined': 0.23; 'bit': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'wondering': 0.25; "i've": 0.25; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'idea': 0.28; 'skip:u 20': 0.28; 'looks': 0.29; 'category:': 0.29; 'operators': 0.29; 'symbols': 0.29; 'character': 0.29; "i'm": 0.30; 'work.': 0.30; 'probably': 0.31; 'certain': 0.31; "can't": 0.32; 'core': 0.32; 'though,': 0.32; 'maybe': 0.33; 'problem': 0.33; 'url:python': 0.33; 'symbol': 0.33; 'equal': 0.34; 'file': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'besides': 0.35; 'unicode': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'agree': 0.37; 'there,': 0.37; 'starting': 0.37; 'someone': 0.38; 'copying': 0.38; 'why': 0.39; 'sure': 0.39; 'where': 0.40; 'some': 0.40; 'url:3': 0.60; 'john': 0.61; 'today,': 0.62; 'here.': 0.62; 'leaving': 0.63; 'more': 0.63; 'here:': 0.63; 'debate': 0.66; "they're": 0.66; 'here': 0.66; 'letters': 0.67; 'jul': 0.72; 'chrisa': 0.84; 'encounters': 0.84; 'greek': 0.84; 'latin': 0.84; 'question!': 0.84; 'western': 0.89; 'to:none': 0.91; 'url:reference': 0.91; 'grey': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc :content-transfer-encoding; bh=TD3CHKpdYURtroz9x1dDxednFjJW3o1BMFnI9ECSKP4=; b=O1wbU+UKCeiIS3paSD/yuH64eCsO+m8GuuWGYMBX8FkCaz0kgGhGSM8E7iqzTv5Ndj mlgphLyHipENgvgpkIkxdmJokA+USixDuBWLY/OfCQP+qYKIeRZV+NxVORVbyGpZFBgd fGOUiydedN43UJOAq7uDnYMgW7/26w463qKtQahbTHLJncmLDVmJq/VhNIWmZUEghoUm GpAFoXp0nWKfE01aqROcuwJM8pjoEP4LoUE+vEEdsIxKic7c7UEjY91hQjCht7Y0UdEj 6NWrwRsj5DJ0TMtcM/Cot2OepMu+X7iF875+IBsotR1XqHoRadqkn8eNENm1nMzlbHpA 5BrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc:content-transfer-encoding; bh=TD3CHKpdYURtroz9x1dDxednFjJW3o1BMFnI9ECSKP4=; b=AARD7PktzjSXZnN2M4qbbE1J0FWR9pnIz9mWNg98/LbbQ7ixn+lxppBpPJ2fy6e6fL /4A/Lqd1ebsb0PDsjqvwKWg0xPcJYZuIm9USPtoh0vNcoLAaarcGX0tp+1CjYIqtkBv6 Od1WEkvy3eLg1U4gejmbncCxrH96gHIiebDUwPpcVbbe8856nqPllTSreUM1QnBdwv0m TWdzPGSiH0JoK1/5BilASUv3GrUYYCXlgElBk8VW38oUOk7o21X3BYsz/wUn9H0GDyLu i6VbC0spuGDSxAGVrjoa//E+Ye6zcTXJLhTk/xnpyQQrvCasM+WGAW5Im6zU9cdrNg6J aT2g== X-Gm-Message-State: ALyK8tKaAMVZCJrP2QO3fb4W9BaYnZO8sNnIfAPbZgrucurqHaU69dlIN3JZN9Evc5BxvL3lsKCviHm7Ai4XlA== X-Received: by 10.194.70.41 with SMTP id j9mr5224707wju.30.1467531719835; Sun, 03 Jul 2016 00:41:59 -0700 (PDT) In-Reply-To: <0589521b-648a-47c1-8afa-6f746e0fb901@googlegroups.com> 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: X-Mailman-Original-References: <0589521b-648a-47c1-8afa-6f746e0fb901@googlegroups.com> Xref: csiph.com comp.lang.python:110983 On Sun, Jul 3, 2016 at 4:58 PM, John Ladasky w= rote: > Up until today, every character I've tried has been accepted by the Pytho= n interpreter as a legitimate character for inclusion in a variable name. = Now I'm copying a formula which defines a gradient. The nabla symbol (=E2= =88=87) is used in the naming of gradients. Python isn't having it. The i= nterpreter throws a "SyntaxError: invalid character in identifier" when it = encounters the =E2=88=87. > > I am now wondering what constitutes a valid character for an identifier, = and how they were chosen. Obviously, the Western alphabet and standard Gre= ek letters work. I just tried a few very weird characters from the Latin E= xtended range, and some Cyrillic characters. These are also fine. > Very good question! The detaily answer is here: https://docs.python.org/3/reference/lexical_analysis.html#identifiers > A philosophical question. Why should any character be excluded from a va= riable name, besides the fact that it might also be an operator? > In a way, that's exactly what's happening here. Python permits certain categories of character as identifiers, leaving other categories available for operators. Even though there aren't any non-ASCII operators in a vanilla CPython, it's plausible that someone could create a Python-based language with more operators (eg =E2=89=A0 NOT EQUAL = TO as an alternative to !=3D), and I'm sure you'd agree that saying "=E2=89=A0= =3D 1" is nonsensical. > This might be a problem I can solve, I'm not sure. Is there a file that = the Python interpreter refers to which defines the accepted variable name c= haracters? Perhaps I could just add =E2=88=87. > The key here is its Unicode category: >>> unicodedata.category("=E2=88=87") 'Sm' You could probably hack CPython to include Sm, and maybe Sc, Sk, and So, as valid identifier characters. I'm not sure where, though, and I've just spent a good bit of time delving (it's based on the XID_Start and XID_Continue derived properties, but I have no idea where they're defined - Tools/unicode/makeunicodedata.py looks promising, but even there, I can't find it). And - or maybe instead - you could appeal to the core devs to have the category/ies in question added to the official Python spec. Symbols like that are a bit of a grey area, so you may find that you're starting a huge debate :) Have fun. ChrisA