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 12:23:34 +1100 Lines: 43 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 /axgPW0hdv666Z3/1RxsawfXLR+d/SKvxSuj+6E3FV6g== 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; 'python,': 0.02; 'else:': 0.03; 'elif': 0.04; 'subject:Python': 0.05; 'cpython': 0.05; 'expressions': 0.07; 'repeated': 0.07; 'cc:addr:python-list': 0.09; 'here?': 0.09; 'lookup': 0.09; 'index': 0.13; "(i'm": 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'opcode': 0.16; 'provision': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'not,': 0.22; 'amounts': 0.22; "python's": 0.23; 'this:': 0.23; 'written': 0.24; 'header:In- Reply-To:1': 0.24; 'testing': 0.25; "i've": 0.25; 'switch': 0.27; 'message-id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'values': 0.28; 'dictionary': 0.29; 'hash': 0.29; 'received:209.85.213.174': 0.29; 'that.': 0.30; 'work.': 0.30; 'code': 0.30; '15,': 0.30; 'branch': 0.30; 'anyone': 0.32; 'implement': 0.32; 'table': 0.32; 'getting': 0.33; 'jump': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'label': 0.35; 'maps': 0.35; 'but': 0.36; 'needed': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'or,': 0.38; 'does': 0.39; 'total': 0.62; 'details': 0.62; 'more': 0.63; 'within': 0.64; 'mar': 0.65; "they're": 0.66; 'obvious': 0.76; 'chrisa': 0.84; 'yourself...': 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=HFyI5PCDfGROo7f1DXJxVFF0lEw0geuA0UFUGzlgjjg=; b=k5DRSWOVxoaq+JA0qwEb0P5QyGn2JrxPIYe0Xd+nV88nXRqWbtNz59HhLzn0v6aEkR jbXNAAjjBMYKkYcwqdLzTLhgSTPOaRkCSRqqR7iI13VBcEm5jW16N5UdWyDmaCtY2qy3 TlnYjv/o601yo2aaHFVpGPTin81DcVBH6297CVHs1qd8eposfaWQmJ7J7a3gNLP4muNi masV55V0hOK0t2nlepSksEbcJRQ7QjxvXSAnQDVbBfjNnkbgZYqeTHc6y2TQ53yVvLA7 /UKkt/dDyrQtBszcI9AMYUpaO9ol+zaQc5Yy8IAX0Cc+hIiYl9wUW+mNGkFpDwVYbwMG DP3w== 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=HFyI5PCDfGROo7f1DXJxVFF0lEw0geuA0UFUGzlgjjg=; b=hQoRit130qJRuGaO/5CHKZv9awoHK3PNMNBsu4qZj4eCHYwth7L3DNVnuniPV2/95/ 5SbvXfDLZl5ut4CMpoKfiVQ1+25DxoYJHEQMruO3QyLeN8BWlSl6cTacN7CbS/JKw4r1 G5uCCVz928Xx0i3cApL9KyEB3AtO9wpmohZIWxpUWIMPvh4hK4AqVZ3Q4XhSrfK4WRnA 0TjXsZIqC3OTNURvVp3hYdHnaby/hRk2qEOctW7PPWCLQiCotW9ggqB+PbWDLmVkasR7 HILz7D6fV3vy9rPzHIWuOCuAO9Y91ibLNcIUPW7Zvou11GD04bh1jjic0eX+FRgGmlwQ 8uGw== X-Gm-Message-State: AD7BkJLYrrvuEVvLhChk/fEd1cse4agsLUI5RU1SNQX95pRCloEqaNmyqyYzW9G1qjCUSdRfxZniLErPtwzv0Q== X-Received: by 10.50.43.168 with SMTP id x8mr21171574igl.92.1458005014492; Mon, 14 Mar 2016 18:23:34 -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:104897 On Tue, Mar 15, 2016 at 12:10 PM, BartC wrote: > The one-byte-code switch works when all case expressions are known at > compile-time. It makes use of a jump-table within the byte-code. > > The total sequence will be more than one byte-code, typically: > > LOAD_FAST The index > SWITCH Jump to the right label > .... > L5: One of multiple labels > ... Deal with the code in this branch > JUMP_ABSOLUTE Break out of the switch > > ... Provision is needed for the jump-table > > But only one is needed for testing and dispatch. Now I've sketched it out, > perhaps you can fill in the details for yourself... (I'm not getting > involved in CPython development.) Let's be fair here; anyone can make a single byte-code that does arbitrary amounts of work. (Consider Python's CALL_FUNCTION or the old PRINT_ITEM opcode.) How much work does SWITCH do here? Does the jump table require that the possible case values be compact? If they're not, how does it operate? In Python, the most obvious way to do the repeated comparisons would be a hash lookup. The SWITCH opcode could take a dictionary that maps values to jump targets, with an 'else' target if the lookup fails, and jump to that. It would actually be possible to implement this entirely in an optimizer, with code written like this: if x == 'asdf': ... elif x == 'qwer': ... elif x in ('zxcv', '1234'): ... else: ... Or, of course, it could get its own syntax. ChrisA