Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cpython': 0.05; 'interpreter': 0.05; 'subject:Python': 0.06; 'compiler': 0.07; 'assumed': 0.09; 'assuming': 0.09; 'optimizing': 0.09; 'python': 0.11; 'def': 0.12; 'foo()': 0.16; 'foo():': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'janssen': 0.16; 'made,': 0.16; 'subject:Java': 0.16; 'wrote:': 0.18; 'written,': 0.19; 'written': 0.21; '>>>': 0.22; 'instance,': 0.24; '15,': 0.26; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'operations.': 0.31; 'safely': 0.31; 'subject: (': 0.35; '(2)': 0.35; 'objects': 0.35; 'operations': 0.35; 'received:google.com': 0.35; 'executing': 0.36; 'turn': 0.37; 'two': 0.37; '(3)': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'easy': 0.60; 'relations': 0.60; 'no.': 0.61; 'simple': 0.61; 'costs': 0.63; 'between': 0.67; 'stored,': 0.84; 'suited': 0.93; '2013': 0.98 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:to :content-type; bh=cTdkW+kjX5P6CsxSxHmkAJ93o26VGzPtlcsew0WGRoU=; b=Qmj4DRwoIk0GWmuLZ3kEtbeVs6Dl9e8g+LBuJT7eHKWoDgfb/BbbBkyxz6/LT2T+0Q Af5fNbdO7M1Nbk5el70QmSzWJzG9VBcZOwqSydHArUZN81JxDRErNKDV3951IVn/Vmg3 Fb2GjPs57vqxWvu6l5kh9iBLfyhYuiz9CL6nIoiKae5IL06l4FRZaE9lhkZxUQ+6FKEk ARmDAAMbUz6aMv/WoIwnPoHSJjInCQOelkUgAWpWunusGw4Wfa6oUogvNlGuMEV5g/aT I+qk/QgCHsIZ74RTwinj8kMDrt16xfHtYUZkpifBchz0PlMBwtSyi08OywdQMIuI7vY7 Pz4Q== MIME-Version: 1.0 X-Received: by 10.68.198.68 with SMTP id ja4mr38829670pbc.24.1381801710696; Mon, 14 Oct 2013 18:48:30 -0700 (PDT) In-Reply-To: References: Date: Tue, 15 Oct 2013 12:48:30 +1100 Subject: Re: Python was designed (was Re: Multi-threading in Python vs Java) From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381801713 news.xs4all.nl 15889 [2001:888:2000:d::a6]:57377 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56835 On Tue, Oct 15, 2013 at 12:31 PM, Mark Janssen wrote: >> Python objects have dynamic operations suited >> to a naive interpreter like CPython. > > Naive, no. > "Naive", in this instance, means executing code exactly as written, without optimizing things (and it's not an insult, btw). For instance, a C compiler might turn this into simple register operations: int x=5; int foo() { x+=3; return x*2; } The two references to 'x' inside foo() can safely be assumed to be the same 'x', and the value as written by the += MUST be the one used to calculate *2. If you declare x to be volatile, that assumption won't be made, and the interpretation will be naive. Now here's the CPython equivalent: x=5 def foo(): global x x+=3 return x*2 >>> dis.dis(foo) 3 0 LOAD_GLOBAL 0 (x) 3 LOAD_CONST 1 (3) 6 INPLACE_ADD 7 STORE_GLOBAL 0 (x) 4 10 LOAD_GLOBAL 0 (x) 13 LOAD_CONST 2 (2) 16 BINARY_MULTIPLY 17 RETURN_VALUE Note that the global is stored, then reloaded. This is the naive approach, assuming nothing about the relations between operations. It's an easy way to be thread-safe, it just costs performance. ChrisA