Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'class,': 0.07; 'string': 0.09; 'subject:into': 0.09; 'api': 0.11; 'def': 0.12; '"idle"': 0.16; '"is"': 0.16; 'fine.': 0.16; 'finney': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; "party's": 0.16; 'subject:variable': 0.16; 'sure.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'feb': 0.22; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'string,': 0.24; 'connected': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'sets': 0.30; 'code': 0.31; 'breaking': 0.31; "d'aprano": 0.31; 'equality': 0.31; 'steven': 0.31; 'writes:': 0.31; 'class': 0.32; 'another': 0.32; 'checking': 0.33; 'fri,': 0.33; 'implemented': 0.33; '+0200,': 0.36; 'idle': 0.36; 'subject:?': 0.36; 'expected': 0.38; 'message- id:@gmail.com': 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; 'free': 0.61; "you're": 0.61; 'skip:\xe2 10': 0.65; 'connected:': 0.84; 'production,': 0.84; '8bit%:33': 0.91 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Fri, 28 Feb 2014 07:49:25 -0700 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131118 Thunderbird/17.0.11 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Can global variable be passed into Python function? 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> <857g8foc93.fsf@benfinney.id.au> In-Reply-To: <857g8foc93.fsf@benfinney.id.au> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393600303 news.xs4all.nl 2885 [2001:888:2000:d::a6]:45290 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67231 On 02/28/2014 01:46 AM, Ben Finney wrote: > 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. Sure. If he replaced the line if self.state is CONNECTED with if self.state == self.CONNECTED then he is free to change CONNECTED at any time. So yes, "is" is not necessary here. Equality checking works fine.