Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'typing': 0.05; 'subject:Python': 0.06; 'python': 0.08; '21,': 0.09; '>>>>': 0.09; 'variables.': 0.09; 'def': 0.12; 'am,': 0.14; 'received:209.85.214.174': 0.14; 'received:mail- iw0-f174.google.com': 0.14; 'wrote:': 0.14; '"test"': 0.16; '(eg': 0.16; 'angelico': 0.16; 'declaring': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; '(i.e.': 0.17; 'tue,': 0.17; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'code': 0.24; 'function': 0.25; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'idle': 0.29; 'all,': 0.30; 'print': 0.31; 'opinion': 0.32; 'to:addr:python-list': 0.33; "isn't": 0.33; '...': 0.34; 'chris': 0.34; 'function.': 0.35; 'using': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'but': 0.38; 'though': 0.38; 'portion': 0.38; 'subject:: ': 0.38; 'explain': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'feature': 0.40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=QdI5OZjlbN9+Dd71cEdhU8icNe03uLoHezA/mK1usFE=; b=YPVy/0QLuZNgcH11pF6hvDqf+JDmNAiNO5Pse/CnWqcq8nnssqoxuvcR+9oqOr1Pd4 uqWNzF9+yrAPAe59IuUjWR3pE9fhpYPGfejG+tl79iYcqoUk74pphORUMNtZpPcGMAwV kOj/JUSgAOrBMBvqGv8lOLmrgfmqoH0oQIci4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=erGw3aKKAVI6P5FUNqodOawDKuHyN2y8dmGSX6Pgp5Fh35UST+Ngw0TIsUGHv8j0JF b9smWKWGmuzAgepN0W+/NWFopabnitn5hQC1KqfunPCtdOmhspfvWI2uo0xugsidSWND axw/bN5BDPdKSew2Xlt1GYe9+8QrvLLoqhNx8= MIME-Version: 1.0 In-Reply-To: <2f69dda9-9532-4fd1-af06-ba85cc2a0e33@v5g2000yqn.googlegroups.com> References: <2f69dda9-9532-4fd1-af06-ba85cc2a0e33@v5g2000yqn.googlegroups.com> Date: Tue, 21 Jun 2011 08:52:22 +1000 Subject: Re: Python scoping From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 18 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308610346 news.xs4all.nl 49183 [::ffff:82.94.164.166]:56920 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8037 On Tue, Jun 21, 2011 at 8:35 AM, gervaz wrote: > Hi all, can you explain me why this simple function works well (i.e. I > can call the print function using txt) in py > >>>> def test(value): > ... =A0 =A0 if value%5: txt =3D "hello" > ... =A0 =A0 else: txt =3D "test" > ... =A0 =A0 print(txt) It's as though you had "PyObject txt;" at the top of the function. The scope is the function. There's no way (afaik) to make a variable be local to a portion of the function - that's a feature that has to be sacrificed to the simplicity of not declaring variables. In my opinion it's better to declare them, except in interactive code (eg IDLE or just typing "python"). But Python isn't that. Chris Angelico