Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; 'true,': 0.05; 'plenty': 0.07; 'variables': 0.07; 'global,': 0.09; 'immutable': 0.09; 'instance.': 0.09; 'instances.': 0.09; 'objects,': 0.09; 'sake': 0.09; 'subject: [': 0.09; 'sure,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'language.': 0.14; 'agree.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mutable': 0.16; 'namespace;': 0.16; 'python;': 0.16; '{}"': 0.16; 'flexibility': 0.16; 'weird': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; 'seems': 0.21; 'separate': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'certainly': 0.24; 'instance,': 0.24; 'module,': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'header:In-Reply-To:1': 0.27; 'thus': 0.29; '(like': 0.30; 'needed.': 0.30; 'start,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'usually': 0.31; 'another.': 0.31; 'convenience': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'though.': 0.31; 'class': 0.32; "can't": 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'instances': 0.36; 'level': 0.37; 'easily': 0.37; 'subject:]': 0.38; 'whatever': 0.38; 'pm,': 0.38; 'that,': 0.38; 'expect': 0.39; 'even': 0.60; 'truly': 0.60; 'new': 0.61; 'entire': 0.61; 'side': 0.67; '26,': 0.68; 'friends': 0.81; 'affected.': 0.84; 'beats': 0.84; 'idiom': 0.84; 'isolate': 0.84; 'level"': 0.84; 'prefers': 0.84; 'subject:Lists': 0.91; 'to:none': 0.92; 'state.': 0.95 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 :content-type; bh=DntlMGKm9lUO8oUZVaiVpkuMllZpNJYDe0GuGrmYrKA=; b=nTRRkhLuC84w0oBkuFCVrV3a4KdwX13hTj73Mu7AsVC7L0gf3bmAlCGqhYof2zlTnY +8Irf3WfWr/Ke4V1OGE26FHXdoFRkV9CJZYDIbgvJOl9PQc2M4y8mDRaeZYKUNjo3Qtg bwdO/LAnfqlmVR/TSH0ENhW13Knpafb8v+wIIPwhVQn+lpqNEIS3cUnmPsJ99BM+9pzz yqoovbfobAycRhG3jmA3/yVXKQaVSikwUP7YF5lZCen6bqKPOuAgYCbBtyitUfzB6e/z cb1gUNsvXgjMfl5inUv3qrF9YvXTTIuDcf235MlzxLaQm7Ohx238LCxLZPYSV31WBXuK XGfg== MIME-Version: 1.0 X-Received: by 10.50.110.4 with SMTP id hw4mr14044931igb.14.1414305726735; Sat, 25 Oct 2014 23:42:06 -0700 (PDT) In-Reply-To: <544c90c6$0$12986$c3e8da3$5496439d@news.astraweb.com> References: <185ea3d5-71d3-4009-8afe-4f0c2b6bbf30@googlegroups.com> <25c6a468-e596-4a31-856b-bb5fe22c8894@googlegroups.com> <9dd6db37-5568-405e-8af7-3a28195eec4d@googlegroups.com> <544c90c6$0$12986$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 26 Oct 2014 17:42:06 +1100 Subject: Re: Side-effects [was Re: I am out of trial and error again Lists] From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414305729 news.xs4all.nl 2870 [2001:888:2000:d::a6]:45395 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79706 On Sun, Oct 26, 2014 at 5:12 PM, Steven D'Aprano wrote: > However, mutator methods on a class don't change global state, they change > the state of an instance. Even random.random and friends don't change > global state, they change a (hidden) instance, and you can create your own > instances when needed. That gives you the convenience of pseudo-global > state while still allowing the flexibility of having separate instances. > Even global variables in Python are global to the module, not global to the > entire application. True, but they do still change state; I oversimplified a bit, but certainly there are plenty of functions that change state in some way, and are not frowned upon. Module level variables really are global, though. You can't easily and conveniently reinstantiate a module in Python; if you "import random; random.seed(1234)", you can confidently expect that some other module that uses random.random will be affected. Sure, there are ways around that, but that's true of any form of global state - for a start, it's usually "process level" state, in that spawning a new process will isolate one from another. The only thing that isn't truly global is the namespace; I can write "x = {}" and you can write "x = []" and they don't collide. They're still global (process-level), it's just that they're "foo.x" and "bar.x" and are thus distinct. > I would say that Python is a pragmatic > language which uses whatever idiom seems best at the time, but over all it > prefers mutable state for compound objects, prefers immutable state for > scalar objects (like numbers), and discourages the unnecessary use of > global mutable state. It's not really compound vs scalar, but yes, I agree. Practicality beats purity, on so many levels. Functions with side effects aren't considered some sort of weird beast that we have to permit for the sake of I/O; they're a fundamental part of the language. ChrisA