Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Encapsulation in Python Date: Tue, 15 Mar 2016 11:12:26 +1100 Lines: 69 Message-ID: References: <56E17985.7060002@benmezger.nl> <6984dafc-fffd-4d46-a173-421bb5c142d2@googlegroups.com> <4ddc3696-3f71-4598-98a6-929267f51fb9@googlegroups.com> <56e53cd3$0$1586$c3e8da3$5496439d@news.astraweb.com> <1376c684-2e7f-417a-9683-e47789f019fe@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de NwWa719BXOV0NRnHBhxVqwxsd5ursC6LKUmQrG4ao75w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python,': 0.02; 'string.': 0.04; 'subject:Python': 0.05; 'cc:addr:python-list': 0.09; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'brackets': 0.09; 'literal': 0.09; 'obj': 0.09; 'python:': 0.09; 'semantics': 0.09; 'python': 0.10; 'def': 0.13; '2016': 0.16; 'colons': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integers,': 0.16; 'quoted': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'stop):': 0.16; 'utterly': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'either.': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'testing': 0.25; 'define': 0.27; 'switch': 0.27; 'message- id:@mail.gmail.com': 0.27; 'restrict': 0.27; 'start,': 0.27; 'values': 0.28; 'equality': 0.29; 'notation': 0.29; 'there.': 0.30; "i'm": 0.30; 'work.': 0.30; '15,': 0.30; 'checks': 0.30; 'skip:_ 10': 0.32; 'implement': 0.32; 'statement': 0.32; 'class': 0.33; "skip:' 20": 0.34; 'tue,': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'clear': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'cases': 0.36; 'success.': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:209.85.213': 0.37; 'requirement': 0.37; 'difference': 0.38; 'received:209': 0.38; 'anything': 0.38; 'or,': 0.38; 'end': 0.39; 'where': 0.40; 'forget': 0.60; 'your': 0.60; 'skip:u 10': 0.61; 'entire': 0.61; 'different': 0.63; 'complete': 0.63; 'within': 0.64; 'between': 0.65; 'mar': 0.65; 'here': 0.66; 'lack': 0.76; 'introduce': 0.79; "'when'": 0.84; "(don't": 0.84; 'chrisa': 0.84; 'coupled': 0.84; "it'd": 0.84; 'of?': 0.84; 'presumably': 0.84; 'syntax:': 0.84; 'terrible': 0.84; 'unacceptable': 0.84; 'to:none': 0.91; 'subject:skip:E 10': 0.96 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; bh=zaQLt8aJAC09PY4WgY9FbWXYURLNz1PXVtITRuCKwXE=; b=BklqYgtvwHftfWZgWOQ/MCARfpF4Q/ZGSsPmeq/LexWCXm7AkTiQpi3SQbw2x6YCg7 1kiaTkK2SkdnnMLTeQgPw2Ed3adbD5sCZbVA8YoWr/Dg08/tR/gD58LL+WXwpcErNZRr KNmW2DrswK3sHSQJeHl4GcUGkL2/yRlO+6sBY4eGFCpuW7l7cmWRXv8RMVZRGvEbMIln Y3Tsmkl7Z+6msrHc42qu32qT44cxEG8vO9o07oftYs6FyjLuQ35QVhXgpKzUgP9QObUR oyKgXNCYhtV4F+Vajpt3fIt1KJxX0704X9We0Zz/pi89HMF/dfN+XjkL2f5ahm5UP0OC uJpQ== 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:date :message-id:subject:from:cc; bh=zaQLt8aJAC09PY4WgY9FbWXYURLNz1PXVtITRuCKwXE=; b=Zulns8vCf05PVlP2if5ZihtB5jbIcOOYKRKACGfFF5cJNQVu+KAOsdebx48Mhr1mib YxKhS7wrIwtI4Rv6M+75C0i6diKB3CUUDHbRxNiYhB2pRFg7S3iLfwBog+Sx+1GTiRkz r5tXFztWs24JgIJRBJPraTqIauByuQ/5Fx35VTuBmTH1SBZ/9GR8kFc8wASCawO+03kv 9sD1wVvZhg2kkwuNbGS/5DWJeW0u+MUiSq4v6tKN+6VYS1pDA42qL14nUb8Gz3d7Dz89 N9dmE0YaU/Cn0fEmYRpRAlaTajKKYp603ACZepxxx4grMxf+y4uV1BkzERenBK6moOLD Ly4Q== X-Gm-Message-State: AD7BkJLADWdFrHF6pjhUpkcFwCngaCQELMPF44SpAxXOHBEWlF25+In/U68yjMTjpA8u6IVraZpMOb224ocCBw== X-Received: by 10.50.137.35 with SMTP id qf3mr20984874igb.92.1458000746806; Mon, 14 Mar 2016 17:12:26 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104879 On Tue, Mar 15, 2016 at 10:56 AM, BartC wrote: > I disagree. And presumably so do others as there are so many different > attempts to implement switch, with varying degrees of success. Here's how I > do it outside Python: > > switch c > when 'A'..'Z','a'..'z','_' then > ++name > when '0'..'9' then > ++numeric > when "()[]<>{}" then > ++brackets > else > ++other > end > > Anything so terrible about that that Python needs to keep well clear of or > that you think its users should be deprived of? Yes: the complete lack of colons at the ends of lines that introduce suites. This is utterly unacceptable in Python! Seriously though - the one thing that I'm very concerned about here is your brackets one. In Python, there's no reason to restrict 'switch' to integers, so I would expect its semantics to be based on either equality comparisons or inequality comparisons (possibly with a requirement that all values be hashable); you could have something like "when in " but a simple "when " would look to me like it's testing for that entire string. (Don't forget that, in Python, there's no difference between 'single quoted strings' and "double quoted strings", as there is in C-like languages.) Although... come to think of it, you could actually define it the other way around: the 'when' statement checks if the current switch object is contained within any of its arguments. Coupled with a range-like object with its own literal syntax, that would pretty much give you what you're looking at there. However, you'd need to have an awkward notation for the cases where you _are_ looking for equality: switch c: when 'A'..'Z', 'a'..'z', ['_']: ++name Or, in a long tree: switch c: when [1]: ... when [2]: ... when [3]: ... Which could work. It'd be well-defined, and not unreasonable. It would, however, be entirely predicated on a "comparison range" object with a literal syntax: class Range: def __init__(self, start, stop): self.start = start self.stop = stop def __contains__(self, obj): return self.start <= obj <= self.stop def __repr__(self): return "%r..%r" % (self.start, self.stop) But that's not overly hard either. ChrisA