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


Groups > comp.lang.python > #8037

Re: Python scoping

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 <rosuav@gmail.com>
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 <rosuav@gmail.com>
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 <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.200.1308610345.1164.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Tue, Jun 21, 2011 at 8:35 AM, gervaz <gervaz@gmail.com> 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):
> ...     if value%5: txt = "hello"
> ...     else: txt = "test"
> ...     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

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


Thread

Python scoping gervaz <gervaz@gmail.com> - 2011-06-20 15:35 -0700
  Re: Python scoping Chris Angelico <rosuav@gmail.com> - 2011-06-21 08:52 +1000
  Re: Python scoping Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 10:39 +1000
    Re: Python scoping Chris Angelico <rosuav@gmail.com> - 2011-06-21 10:55 +1000
      Re: Python scoping Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 12:38 +1000
        Re: Python scoping Chris Angelico <rosuav@gmail.com> - 2011-06-21 13:21 +1000
          Re: Python scoping Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 14:06 +1000
            Re: Python scoping gervaz <gervaz@gmail.com> - 2011-06-21 02:05 -0700
  Re: Python scoping Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-21 00:57 +0000

csiph-web