Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'class,': 0.07; 'string': 0.09; 'caller': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; '"idle"': 0.16; 'enum': 0.16; 'equality.': 0.16; 'finney': 0.16; 'hack,': 0.16; "party's": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:variable': 0.16; 'url:peps': 0.16; 'values:': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'later': 0.20; 'feb': 0.22; 'header :User-Agent:1': 0.23; 'comparing': 0.24; 'module,': 0.24; 'string,': 0.24; 'url:dev': 0.24; 'connected': 0.24; 'second': 0.26; 'primary': 0.26; 'values': 0.27; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; "we'd": 0.29; 'compared': 0.30; 'sets': 0.30; "i'm": 0.30; 'code': 0.31; 'breaking': 0.31; "d'aprano": 0.31; 'motivation': 0.31; 'steven': 0.31; 'writes:': 0.31; 'class': 0.32; 'another': 0.32; 'text': 0.33; 'url:python': 0.33; 'fri,': 0.33; 'implemented': 0.33; 'but': 0.35; 'there': 0.35; '+0200,': 0.36; 'idle': 0.36; 'received:com.au': 0.36; 'done': 0.36; "didn't": 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'so,': 0.37; 'two': 0.37; 'clear': 0.37; 'expected': 0.38; 'ben': 0.38; 'writes': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'subject:Can': 0.60; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'making': 0.63; 'skip:\xe2 10': 0.65; '8bit%:43': 0.74; '3.4': 0.84; 'brain,': 0.84; 'connected:': 0.84; 'production,': 0.84; 'received:125': 0.84; '8bit%:33': 0.91; 'hand,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Can global variable be passed into Python function? Date: Fri, 28 Feb 2014 19:46:48 +1100 References: <27ac2248-0ca3-4ba6-9d25-eaad324bc5e9@googlegroups.com> <5f4f5a5f-327a-4616-8235-17ee9e74c488@googlegroups.com> <530fef58$0$11113$c3e8da3@news.astraweb.com> <871tynznpd.fsf@elektro.pacujo.net> <53104798$0$11113$c3e8da3@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: vmx15867.hosting24.com.au User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) X-Public-Key-ID: 0xBD41714B X-Public-Key-Fingerprint: 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-gpg.asc X-Post-From: Ben Finney Cancel-Lock: sha1:7hS7Pe89VxtziMT3D74ORLwvIEk= 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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393577227 news.xs4all.nl 2893 [2001:888:2000:d::a6]:58671 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67207 Steven D'Aprano writes: > On Fri, 28 Feb 2014 09:43:58 +0200, Marko Rauhamaa wrote: > > class Connection: > > IDLE = "IDLE" > [...] > > CONNECTED = "CONNECTED" > [...] > > def disconnect(self): > > ... > > if self.state is CONNECTED: > > ... > > Why do you care that the state is *that specific* string, rather than > any old string with the value "CONNECTED"? I can think of a reason: * When you publish the API for the ‘Connection’ class, * and another party writes code that sets ‘state’ to a string with the value ‘"CONNECTED"’, * and you implemented the check as ‘self.state == "CONNECTED"’, * and their code works with your class and it goes into production, * you're then not able to change the expected value without breaking that party's code. On the other hand, if the only value which will work is the one which the caller gets via ‘Connection.CONNECTED’, then you can change the implementation later without breaking existing code. There are two reasons why I think this is *still* not a justification for using ‘is’ with string values: First reason: This is better done by making it clear the value is an arbitrary object that won't be compared for equality. Just use ‘object()’ to creeate each value and be done with it. That's a hack, but it's better than pretending you'll use the string as a string of text and then breaking that expectation. Second reason: This use case is a primary motivation for the Enum pattern. The Enum pattern is implemented in the standard-library ‘enum’ module, now in Python 3.4 . So, I think Marko's use case is not a justification for comparing string values with ‘is’. -- \ “Pinky, are you pondering what I'm pondering?” “Wuh, I think | `\ so, Brain, but if we didn't have ears, we'd look like weasels.” | _o__) —_Pinky and The Brain_ | Ben Finney