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


Groups > comp.lang.python > #6566

Re: scope of function parameters

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'instance,': 0.05; 'fine,': 0.07; 'function,': 0.07; 'though.': 0.07; 'worse': 0.07; 'python': 0.08; '(int': 0.09; 'exceptions': 0.09; 'foo': 0.09; "it'd": 0.09; 'it;': 0.09; 'subclass': 0.09; 'subject:parameters': 0.09; 'thrown': 0.09; 'variables.': 0.09; 'this:': 0.10; 'def': 0.12; 'c++': 0.12; 'exception': 0.12; 'am,': 0.14; 'received:209.85.214.174': 0.14; 'received:mail- iw0-f174.google.com': 0.14; 'wrote:': 0.14; 'angelico': 0.16; 'confuses': 0.16; 'declaring': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guessing': 0.16; 'local.': 0.16; 'nameerror': 0.16; 'subject:function': 0.16; 'subsequent': 0.16; 'mon,': 0.17; 'somewhere': 0.17; 'meant': 0.18; 'generic': 0.19; 'issue,': 0.19; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'seems': 0.21; 'loop': 0.22; 'loop,': 0.23; 'worst': 0.23; "doesn't": 0.25; 'string': 0.26; 'pass': 0.27; 'says': 0.27; 'message-id:@mail.gmail.com': 0.28; '(the': 0.28; 'received:209.85.214': 0.28; 'concern': 0.29; 'odd': 0.29; "python's": 0.29; 'variables': 0.29; 'bit': 0.30; 'least': 0.30; 'fact': 0.30; 'fairly': 0.30; 'catching': 0.30; 'steven': 0.32; 'to:addr:python-list': 0.33; "isn't": 0.33; 'error': 0.33; '...': 0.34; 'chris': 0.34; 'crazy': 0.35; "d'aprano": 0.35; 'explicit': 0.35; 'function.': 0.35; 'else': 0.35; 'using': 0.35; 'reference': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'but': 0.38; 'easiest': 0.38; 'subject:: ': 0.38; 'doing': 0.39; 'received:209': 0.39; 'goes': 0.39; 'to:addr:python.org': 0.39; '"for': 0.67; 'yourself': 0.68; '30,': 0.84; 'masking': 0.84; 'zen': 0.84; 'wait,': 0.91
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; bh=F9rr/9B5tfCFR2hmKf0dl0ma4EcQHSKxMZuvCaxqtzE=; b=JSYAX1AUte4bn5iu7E/IV9RgX9cwpkEO6XCvkX1TLmeAmisTRHXcb9AyVTQTS1brKO Tu6SeTmrpOX7R/IVZaCulYoDs0CRUwSnikzKuWTnRzDbEm76izcH/0v/5JFGbdATvIjw V3VM0dpGvp2L4BjPnPD9Y7xUFcJjJpcLA6osg=
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; b=v8Y7OK4j11SXy7daj4YJ3XRFQKmlZ9UGRajTUJuy0ml+zoM5/nnZ5unp86ia5t7APQ zfWYkRBjLbKmthNz7nodX6Q0tN+xn+bdAAHX2q5CHCNWB9RnBY06ZIyc45xKk5spsh6d NJOwOhVRJvLKoSt5+Ez2Z4NmX2O8JvScsl2AE=
MIME-Version 1.0
In-Reply-To <4de29612$0$29996$c3e8da3$5496439d@news.astraweb.com>
References <F8395F78-615E-4FBD-B6FC-1D6173EAEA45@mcgill.ca> <mailman.2217.1306662671.9059.python-list@python.org> <4de24045$0$29996$c3e8da3$5496439d@news.astraweb.com> <BANLkTinK=dHCbocURD2+s5uvdonBkS+uFQ@mail.gmail.com> <BANLkTimWP+jP0s5DsBaFbv6LV5OTYA8hQw@mail.gmail.com> <mailman.2233.1306694309.9059.python-list@python.org> <4de29612$0$29996$c3e8da3$5496439d@news.astraweb.com>
Date Mon, 30 May 2011 05:20:51 +1000
Subject Re: scope of function parameters
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.2235.1306696854.9059.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 82.94.164.166
X-Trace 1306696854 news.xs4all.nl 49184 [::ffff:82.94.164.166]:47177
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6566

Show key headers only | View raw


On Mon, May 30, 2011 at 4:53 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> UnboundLocalError is a subclass of NameError, so it will still be caught
> by try...except NameError.
>
> If you're crazy enough to be catching NameError :)

Ah okay. So it is still NameError, it just doesn't look like one.

> While UnboundLocalError is jargon, and not the easiest error message to
> comprehend, at least it confuses in a different way :)

I have nothing against jargon, and specific errors are better than
generic ones (imagine if every error were thrown as Exception with a
string parameter... oh wait, that's what string exceptions are).

It still seems a little odd that a subsequent line can affect this
one. But Python's mostly doing what would be expected of it; the worst
I can come up with is this:

def f():
  print(foo) # reference a global
  ...
  for foo in bar: # variable only used in loop
    pass

If you're used to C++ and declaring variables inside a for loop eg
"for (int i=0;i<10;++i)", you might not concern yourself with the fact
that 'foo' is masking a global; it's not an issue, because you don't
need that global inside that loop, right? And it would be fine, except
that that global IS used somewhere else in the function. It'd be a bit
confusing to get the UnboundLocalError up on the print(foo) line (the
one that's meant to be using the global), since that line isn't wrong;
and the "obvious fix", adding an explicit "global foo" to the top of
the function, would be worse (because it would mean that the for loop
overwrites the global).

This is why I would prefer to declare variables. The Zen of Python
says that explicit is better than implicit, but in this instance,
Python goes for DWIM, guessing whether you meant global or local. It
guesses fairly well, though.

Chris Angelico

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


Thread

Re: scope of function parameters Wolfgang Rohdewald <wolfgang@rohdewald.de> - 2011-05-29 11:47 +0200
  Re: scope of function parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-29 12:47 +0000
    Re: scope of function parameters Chris Angelico <rosuav@gmail.com> - 2011-05-30 03:53 +1000
      Re: scope of function parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-29 18:28 +0000
    Re: scope of function parameters Chris Rebert <clp2@rebertia.com> - 2011-05-29 11:01 -0700
    Re: scope of function parameters Chris Angelico <rosuav@gmail.com> - 2011-05-30 04:38 +1000
      Re: scope of function parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-29 18:53 +0000
        Re: scope of function parameters Chris Angelico <rosuav@gmail.com> - 2011-05-30 05:20 +1000
    Re: scope of function parameters Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-29 13:12 -0600

csiph-web