Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'string': 0.09; 'formatting': 0.09; 'subject:into': 0.09; 'things,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"idle"': 0.16; '"is"': 0.16; '(either': 0.16; 'disconnected': 0.16; 'enum': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'object()': 0.16; 'safely.': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'seems': 0.21; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'skip:{ 20': 0.24; 'connected': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'accidentally': 0.31; 'object.': 0.31; 'class': 0.32; 'fri,': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'something': 0.35; 'equal': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'idle': 0.36; 'subject:?': 0.36; 'two': 0.37; 'ben': 0.38; 'same.': 0.38; 'pm,': 0.38; 'that,': 0.38; 'subject:Can': 0.60; 'real': 0.63; 'connecting': 0.64; 'become': 0.64; 'more': 0.64; '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=raJnuGA5cktSuZWP6jBT1vkC8wZ8HsGTgOOAJTfFbJk=; b=iEF/VO5R3p67IGJA84fwgO7RCv1IUY9tZOpu4Ukl7ApzdDXSjrc1ecw6X7lmtlBeVA KcSoXP+b/R3VAkq7os3Ry+/I1Wew9keG7sQIkPBJqytA2sw5iq1oujisiitL29EabEW6 OZQRYggK/FC1rwrr5EKcEO1hyh2Wcp65aAxQDYj+gl2AMdJrkTCcXRgv4oRG+KeTL1nQ kE7QVmNCGrYU7dlkf2I1VcFCsLXQg+jV5xpTd5NGdEXplac3irx2FQZvGi5gVw1naPMp n8vOzSUsaD6WdztBpa7LIcRCtFzmb/IOMdk4WncOm+e7QJZTb58bqa1AVFJO/aLXOmmq NPcQ== MIME-Version: 1.0 X-Received: by 10.68.98.3 with SMTP id ee3mr2202844pbb.31.1393578008053; Fri, 28 Feb 2014 01:00:08 -0800 (PST) In-Reply-To: <871tynznpd.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> Date: Fri, 28 Feb 2014 20:00:07 +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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393578017 news.xs4all.nl 2847 [2001:888:2000:d::a6]:39465 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67210 On Fri, Feb 28, 2014 at 6:43 PM, Marko Rauhamaa wrote: > Here's a use case for "is" with strings (or ints): > > class Connection: > IDLE = "IDLE" > CONNECTING = "CONNECTING" > CONNECTED = "CONNECTED" > DISCONNECTING = "DISCONNECTING" > DISCONNECTED = "DISCONNECTED" > > The state objects could have been defined like this: > > IDLE = object() > CONNECTING = object() > CONNECTED = object() > DISCONNECTING = object() > DISCONNECTED = object() > > However, strings have the advantage in troubleshooting: > > sys.stderr.write("state = {}\n".format(self.state)) As Ben said, strong use-case for enums (either migrate to 3.4, or check PyPI). But here's an alternative that uses object identity safely. (Remember, all it takes is a bit of string interning and two equal strings could become identical.) class enum: def __init__(self, desc): self.desc = desc def __repr__(self): return self.desc IDLE = enum("IDLE") CONNECTING = enum("CONNECTING") CONNECTED = enum("CONNECTED") DISCONNECTING = enum("DISCONNECTING") DISCONNECTED = enum("DISCONNECTED") Now object identity is the right way to do things, and you can still do the formatting just like you say; plus there's no way to accidentally get something that seems to work. Of course, the real enum type is far more sophisticated than that, but the concept is the same. It's an object. ChrisA