Article of interest: Python pros/cons for the enterprise
Arnaud Delobelle
arnodel at googlemail.com
Sun Feb 24 04:52:54 EST 2008
On Feb 24, 8:08 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sat, 23 Feb 2008 19:45:45 -0800, Jeff Schwab wrote:
> The second link is just bizarre. It claims that Python "does not have
> nested, block variable scopes like I am accustomed to in nearly every
> other programming language I use, like C, C#, C++, Objective C, Perl, and
> Java." (Notice the common denominator?) But that's nonsense -- Python
> does have nested block variable scopes. This "pitfall", if it is one, is
> that Python doesn't create a new scope for if-blocks. Coming from a
> Pascal background instead of C, it never even occurred to me that if-
> blocks could be a new scope, or that anyone would possibly want it to be.
I haven't read the link, but it is true that python < 3 is deficient
in that area: names in an enclosing scope can be accessed but not
rebound. In the latter case the interpreter assumes them to be local
names. Python 3000 fixes this with the new 'nonlocal' compilation
directive. E.g.
(python 2.5)
>>> def counter(acc=0, step=1):
... def tick():
... acc += step
... return acc
... return tick
...
>>> c = counter()
>>> c()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in tick
UnboundLocalError: local variable 'acc' referenced before assignment
>>>
(python 3.0)
>> def counter(acc=0, step=1):
... def tick():
... nonlocal acc
... acc += step
... return acc
... return tick
...
>>> c = counter()
>>> c()
1
>>> c()
2
>>>
--
Arnaud
More information about the Python-list
mailing list