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


Groups > comp.lang.python > #6697

Re: scope of function parameters (take two)

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.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; 'example:': 0.03; 'assign': 0.04; 'string,': 0.05; 'char': 0.07; 'fine,': 0.07; 'function,': 0.07; 'names.': 0.07; "shouldn't": 0.07; 'subject:two': 0.07; 'python': 0.08; 'accidentally': 0.09; 'down,': 0.09; 'globals': 0.09; 'integer,': 0.09; 'prevents': 0.09; 'referencing': 0.09; 'subject:parameters': 0.09; 'variables.': 0.09; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; '"int': 0.16; '"private"': 0.16; '*b;': 0.16; 'angelico': 0.16; 'block;': 0.16; 'declaring': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lexical': 0.16; 'narrow': 0.16; 'programmer,': 0.16; 'scope,': 0.16; 'scope.': 0.16; 'subject:function': 0.16; 'unambiguous': 0.16; 'variable.': 0.16; 'float': 0.16; 'forgotten': 0.16; 'sections': 0.16; 'tue,': 0.17; 'language': 0.18; 'programming': 0.19; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'loop': 0.22; 'received:209.85.210.174': 0.23; 'received:mail- iy0-f174.google.com': 0.23; 'stops': 0.23; 'url:wiki': 0.23; 'code': 0.24; "doesn't": 0.25; 'point,': 0.25; 'function': 0.25; "i'm": 0.27; 'message-id:@mail.gmail.com': 0.28; 'language.': 0.28; 'interact': 0.29; 'variables': 0.29; 'cease': 0.30; 'granted,': 0.30; 'modifying': 0.30; 'execution': 0.32; 'to:addr :python-list': 0.33; 'chris': 0.34; 'there': 0.35; 'forces': 0.35; 'latter': 0.35; 'using': 0.35; 'considered': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'url:en': 0.37; 'another': 0.37; 'put': 0.37; 'url:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'doing': 0.39; 'subject: (': 0.39; 'received:209': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; 'simply': 0.60; '31,': 0.65; 'believe': 0.66; 'here': 0.66; 'care': 0.72; '10:28': 0.84; 'distance.': 0.93
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=YCGo4Tzi1XFab19I14mFW6Y/C0GxvtDTc/XFuRfqPvc=; b=ad2jrVHJXI6UN5zaXmguv7p5XGFFHhJjgpSffNSnwpO7zMnV12QojtkRHkRlbnSBcA pPb2YYsRWsjSG2ZHobI2sszeOkz5JFf+lKrLA9yfiTYMhAW4wDnrTcDarRT23KU0tLxR 3L6A/vPI6vI0r4Up5+KlNGVEbif0mNBd3u+Xo=
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=Jy6LNIzo/uz1/ihnhWDXHeuKv67uUAPOrre9RAbNlmuqIWiypLLFxWTlZOIS2yUNs+ doqFE/fktWwx8bKKCMUu/e3Fn+T29o4ULD/HoM4Hv+9ORHW2mKI93uLZeyEeAM5rzcUS TpR5pcI1DunWSO4byLf03BggeCUZltlXt4qNo=
MIME-Version 1.0
In-Reply-To <F4EAD1ED-563D-4D6E-A50C-68308A9F26B7@mcgill.ca>
References <F8395F78-615E-4FBD-B6FC-1D6173EAEA45@mcgill.ca> <F4EAD1ED-563D-4D6E-A50C-68308A9F26B7@mcgill.ca>
Date Tue, 31 May 2011 13:05:25 +1000
Subject Re: scope of function parameters (take two)
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.2291.1306811129.9059.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 82.94.164.166
X-Trace 1306811129 news.xs4all.nl 49177 [::ffff:82.94.164.166]:40815
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6697

Show key headers only | View raw


On Tue, May 31, 2011 at 10:28 AM, Henry Olders <henry.olders@mcgill.ca> wrote:
> I don't believe I'm the only person who thinks this way. Here is a quote from wikipedia: "It is considered good programming practice to make the scope of variables as narrow as feasible so that different parts of a program do not accidentally interact with each other by modifying each other's variables. Doing so also prevents action at a distance. Common techniques for doing so are to have different sections of a program use different namespaces, or to make individual variables "private" through either dynamic variable scoping or lexical variable scoping." (http://en.wikipedia.org/wiki/Variable_(programming)#Scope_and_extent).
>

Side point, on variable scope.

There is a philosophical split between declaring variables and not
declaring them. Python is in the latter camp; you are not required to
put "int a; char *b; float c[4];" before you use the integer, string,
and list/array variables a, b, and c. This simplifies code
significantly, but forces the language to have an unambiguous scoping
that doesn't care where you assign to a variable.

Example:

def f():
  x=1  # x is function-scope
  if cond: # cond is global
    x=2 # Same function-scope x as above
  print(x) # Function-scope, will be 2 if cond is true

This is fine, and is going to be what you want. Another example:

def f():
  print(x) # global
  # .... way down, big function, you've forgotten what you're doing
        for x in list_of_x:
          x.frob()

By using x as a loop index, you've suddenly made it take function
scope, which stops it from referencing the global. Granted, you
shouldn't be using globals with names like 'x', but it's not hard to
have duplication of variable names. As a C programmer, I'm accustomed
to being able to declare a variable in an inner block and have that
variable cease to exist once execution leaves that block; but that
works ONLY if you declare the variables where you want them.

Infinitely-nested scoping is simply one of the casualties of a
non-declarative language.

Chris Angelico

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


Thread

Re: scope of function parameters (take two) Chris Angelico <rosuav@gmail.com> - 2011-05-31 13:05 +1000

csiph-web