Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53008

Re: Python Global variable

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'purpose.': 0.07; 'function,': 0.09; 'global,': 0.09; 'kumar': 0.09; 'python': 0.11; 'def': 0.12; '10:54,': 0.16; 'bind': 0.16; 'declared': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; "haven't": 0.24; 'mention': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'assumes': 0.31; 'anywhere': 0.35; 'but': 0.35; 'method': 0.36; 'to:addr:python-list': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'name': 0.63; 'refer': 0.63; 'different': 0.65; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'therefore': 0.72; 'reply- to:addr:python.org': 0.84; 'verifying': 0.84; 'subject:Global': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=bL0vfpOZ c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=0kkAYlmtguIA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=mDW81V_B-rsA:10 a=7q9WPlesjEhW6_4Pb-kA:9 a=wPNLvfGTeEIA:10
X-AUTH mrabarnett:2500
Date Mon, 26 Aug 2013 17:18:54 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
MIME-Version 1.0
To python-list@python.org
Subject Re: Python Global variable
References <1377510840.32281.YahooMailBasic@web190503.mail.sg3.yahoo.com>
In-Reply-To <1377510840.32281.YahooMailBasic@web190503.mail.sg3.yahoo.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.241.1377533929.19984.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1377533929 news.xs4all.nl 15940 [2001:888:2000:d::a6]:42317
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53008

Show key headers only | View raw


On 26/08/2013 10:54, chandan kumar wrote:
> Hi all,
>
> Please see the below code,in which i'm verifying the global value in python.
>
> CHK=10
>
> def test1():
>      print "Value of  CHK in test1",CHK
>
> def test2():
>      CHK=40
>      print "Value of CHK  in test2",CHK
>      test1()
>
> def test3():
>      global CHK
>      test2()
>
> test3()
>
> When i ran above code ,I'm getting vlaue of CHK as 40 in test2 method and 10 in test1 method
> Can somone explain me why the value of CHK  is different in method test1 and test2.
>
In a function, if you bind to a name then Python assumes that the name
is local to that function unless you declare that it's global.

In test1, you refer to 'CHK' but don't bind to it, therefore it's
global.

In test2, you bind to 'CHK' with 'CHK=40', and you haven't declared
that it's global, therefore it's local to test2.

In test3, you declare that 'CHK' is global, but don't mention it
anywhere else in that function, so it serves no purpose.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python Global variable MRAB <python@mrabarnett.plus.com> - 2013-08-26 17:18 +0100

csiph-web