Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'value,': 0.04; 'true,': 0.05; 'subject:Python': 0.06; '(especially': 0.07; 'compiler': 0.07; 'string': 0.09; 'assuming': 0.09; 'caller': 0.09; 'literal': 0.09; 'merging': 0.09; 'optimizing': 0.09; 'strings.': 0.09; 'subject:into': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'creates': 0.14; '"=="': 0.16; '"int': 0.16; '"is"': 0.16; 'brackets': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'harsh,': 0.16; 'idle,': 0.16; 'integers,': 0.16; 'latter,': 0.16; 'subject:variable': 0.16; 'thread,': 0.16; 'prevent': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'slightly': 0.19; 'value.': 0.19; 'not,': 0.20; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'issue,': 0.24; 'connected': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'correct': 0.29; 'chris': 0.29; 'on,': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'context,': 0.31; "d'aprano": 0.31; 'object.': 0.31; 'steven': 0.31; 'symbolic': 0.31; 'option': 0.32; 'becomes': 0.33; 'not.': 0.33; 'maybe': 0.34; 'could': 0.34; 'anywhere': 0.35; 'created': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'shows': 0.36; 'subject:?': 0.36; 'being': 0.38; 'sometimes': 0.38; 'e.g.': 0.38; 'same.': 0.38; 'rather': 0.38; 'does': 0.39; 'heard': 0.39; 'sure': 0.39; 'either': 0.39; 'expression': 0.60; 'is.': 0.60; 'subject:Can': 0.60; 'new': 0.61; "you're": 0.61; 'such': 0.63; 'happen': 0.63; 'refer': 0.63; 'stand': 0.64; 'more': 0.64; '(that': 0.65; 'worth': 0.66; 'here': 0.66; 'between': 0.67; 'mar': 0.68; 'steady': 0.68; 'safe': 0.72; 'construction': 0.72; 'obvious': 0.74; 'square': 0.74; 'fail.': 0.84; 'intern': 0.84; 'preventing': 0.84; 'significance': 0.84; "they'd": 0.84; 'working,': 0.84; 'absolutely': 0.87; 'crucial': 0.91; 'thing,': 0.91; '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=gRYqzV1L1SW6Mkdl12dh/+OMUWOSAdrSOTFv9djUudI=; b=ZKKL+BRGfSqHYyv2HqoouJUkI2NTAD6fRf8VxdELqR3rdPzXZSQ5zRaqKpQItEEu7a vthH6rReoB574kq5BDQoU4hg1PCxbmwOA4xY5tGPyJvX6GM3OWP7/qEYLuHD5sjfN7tY Zud9CZSk9eHmVr/sM32E2IH6tkP8XADyRSIbs3hrjESy2Bf50lkPKwQPMRYYrGjLtFxO btuQpEe2qS8k1kJBs7SEoloFpoSUGXb3Ct2XvSl97nb8zoxkeh97rTjbgr+IDeST4xI5 dEFsGfOJigHGD+Yuy5VH5z/Efg8RMmmno9zA+ooYAqxJhUwg5RGZq8t5xWjDpleR1NSm 9U+Q== MIME-Version: 1.0 X-Received: by 10.68.204.231 with SMTP id lb7mr10508101pbc.30.1393694848336; Sat, 01 Mar 2014 09:27:28 -0800 (PST) In-Reply-To: <531213c4$0$29987$c3e8da3$5496439d@news.astraweb.com> 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> <87k3ceeq0m.fsf@elektro.pacujo.net> <87zjlad8q4.fsf@elektro.pacujo.net> <874n3irz04.fsf@elektro.pacujo.net> <87k3ceqhti.fsf@elektro.pacujo.net> <531213c4$0$29987$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 2 Mar 2014 04:27:28 +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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393694852 news.xs4all.nl 2934 [2001:888:2000:d::a6]:50959 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67324 On Sun, Mar 2, 2014 at 4:07 AM, Steven D'Aprano wrote: > On Sat, 01 Mar 2014 22:59:52 +1100, Chris Angelico wrote: >> And, as proven here in this thread, you do not know what you are doing. > > Steady on, that's a bit harsh. In context, I think that Marko is assuming > that the caller will only ever use the state values via their symbolic > names, e.g. only refer to them as IDLE, CONNECTED etc. and never use > their internal string values "IDLE", "CONNECTED". A bit harsh, perhaps, but I stand by it: by repeatedly declaring that something is safe when it has been proven not safe, he shows that he does not know what he is doing - that he is not fully comprehending the significance of object value vs identity as regards strings. Incidentally, Python somewhat creates this issue, by sometimes interning and sometimes not. (And the same with small integers, although I've never heard the expression "int interning".) If strings were always interned, then it would be obvious that identity and value were one and the same. On the flip side, if a string literal always created a new string (that is, if it were more like a "string construction syntax" like square brackets for lists), then it would be obvious that each one is a separate object. I don't say either option is worth doing (especially the latter, O!), but by sometimes merging and sometimes not, Python does slightly blur the line between identity and value. (Obviously if strings were mutable, they'd *have* to have separate identities.) > I don't think that's a safe assumption, since it requires the caller to > only ever do the right thing, but if it is true, then using "is" in the > way he suggests cannot fail. If you're depending on the caller doing the right thing, it's still safe to use == rather than is. The only reason to use 'is' is to prevent the caller from stuffing in some other string that happens to be correct - hence his point about being able to change the keywords. If the caller stuffs in the string "INIT" instead of the object Foo.INIT, then "==" will happen to work until such time as Foo.INIT becomes "INITIALIZING", at which point it breaks. Preventing that means that unique object identity is crucial to the system working, and that's pretty much impossible with strings. (I don't say it's absolutely impossible; maybe if you intern some other string with the same value, and then construct the string you want out of pieces, and somehow prevent the compiler from figuring out what you're doing and optimizing it all away, then maybe you could have something that doesn't get reused anywhere else. But it's certainly not normal to be able to be sure of that.) ChrisA