Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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; 'python,': 0.02; 'subject:Python': 0.06; 'init': 0.07; 'string': 0.09; 'definition,': 0.09; 'subject:into': 0.09; 'used.': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; '"<>"': 0.16; 'distinct': 0.16; 'enum': 0.16; 'fetch': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'identifiers,': 0.16; 'literals': 0.16; 'objects.': 0.16; 'subject:variable': 0.16; 'prevent': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'normally': 0.19; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'object.': 0.31; 'table,': 0.31; 'class': 0.32; 'fri,': 0.33; 'third': 0.33; 'skip:_ 10': 0.34; "can't": 0.35; 'equal': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; 'module.': 0.36; 'subject:?': 0.36; 'should': 0.36; 'example,': 0.37; 'two': 0.37; 'depends': 0.38; 'pm,': 0.38; 'either': 0.39; 'even': 0.60; 'subject:Can': 0.60; 'then,': 0.60; 'matter': 0.61; 'simply': 0.61; "you're": 0.61; 'first': 0.61; 'different': 0.65; '"one': 0.84; '9:02': 0.84; 'preventing': 0.84; 'safe.': 0.84; 'to:none': 0.92 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=jZhHDiaICxSVkCElxPP+lMAv0fS6R0HRvKFwri/r340=; b=AkuY01Z2/TOFwPDnaIfdZK04vp50su7mn8oiwA2qxsoQtvG6QZB/Nx29GvIUHqdYAD LMjN03B493rtf2X9DsG9+IcbesUfJV+cgGVapsXNiGpp0WiQzF+MPW95yDRXaM/CGlRp 42HjKpDg31/Ccws4AVrzXqo9Us7sxBR5x92STyytCgLSvTRIq6RUv3+bktg6aYwAguXQ j1Psspp0C/mG98+AS0UjZ8oVIUEUfZdg/j1pPNbqty1nYO25JJE4FcaJBA4LCU6xMSRx bsBnEEhhvaNQ22VnurOJFFQN7UWChXcGuMAzIbpUO0BCnHUJ7iqvPXchMIZttyz7zsMi GZ6g== MIME-Version: 1.0 X-Received: by 10.68.98.3 with SMTP id ee3mr2758375pbb.31.1393584954141; Fri, 28 Feb 2014 02:55:54 -0800 (PST) In-Reply-To: <87ha7jy2qs.fsf@elektro.pacujo.net> 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> <87ha7jy2qs.fsf@elektro.pacujo.net> Date: Fri, 28 Feb 2014 21:55:53 +1100 Subject: Re: Can global variable be passed into Python function? 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393584957 news.xs4all.nl 2976 [2001:888:2000:d::a6]:40154 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67213 On Fri, Feb 28, 2014 at 9:02 PM, Marko Rauhamaa wrote: > Yes, enums are long overdue. However, since any distinct objects will > do, there is nothing preventing you from using string objects. > String literals will often be interned if they look like (especially, if they *are*) identifiers, so if you want to prevent other strings from happening to match, you can't trust 'is'. >>> class Foo: INIT = "INIT" def __init__(self): self.state = self.INIT def initializing(self): return self.state is self.INIT >>> a=Foo() >>> a.initializing() True >>> a.state="INIT" >>> a.initializing() True So you should use some string value that doesn't look like an identifier: >>> class Foo: INIT = "<>" def __init__(self): self.state = self.INIT def initializing(self): return self.state is self.INIT >>> a=Foo() >>> a.initializing() True >>> a.state="<>" >>> a.initializing() False But even then, chances are you can force the matter by interning explicitly. >>> class Foo: INIT = ">>INIT<<" def __init__(self): self.state = self.INIT def initializing(self): return self.state is self.INIT >>> a=Foo() >>> a.initializing() True >>> sys.intern(a.state) '>>INIT<<' >>> a.state=sys.intern(">>INIT<<") >>> a.initializing() True Note that in no case did I at all tamper with the class definition, either to change its idea of the INIT string or to fetch that particular object. Two equal strings, in Python, might and might not be identical, and you simply cannot rely on interning either way. The third example, incidentally, depends on sys.intern reusing a.state as the "one interned string". This will normally be what happens if it's the first string of that value to be used. So you might be able to first force the strings to be in the interning table, and then force your sentinels to be different objects. But at that point, you really should be using object(), or a proper enum module. If you're using strings as state values, you should be using == to compare them. Nothing else is safe. ChrisA