Use of a variable in parent loop

Python python at bladeshadow.org
Mon Sep 28 20:48:13 EDT 2020


On Sun, Sep 27, 2020 at 03:18:44PM +0800, Stephane Tougard via Python-list wrote:
> On 2020-09-27, 2QdxY4RzWzUUiLuE at potatochowder.com <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
> > As ChrisA noted, Python almost always Just Works without declarations.
> > If you find yourself with a lot of global and/or nonlocal statements,
> > perhaps you're [still] thinking in another language.
> 
> 
> I don't really agree with that, trying to use an undeclared
> object/variable/whatever :
> 
> Python 3.7.7 (default, Aug 22 2020, 17:07:43) 
> [GCC 7.4.0] on netbsd9
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print(name)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   NameError: name 'name' is not defined
>   >>> 
> 
> You can say it's not the "declaration" the issue, it's the "definition",
> that's just a matter of vocabulary and it does not answer the question.

No it's not... those two words mean different things, and the
difference actually matters.  If you use strict in perl you need BOTH
(at least, if you want your variable to actually have a value):

  my $foo;
  $foo = 1;

The declaration and definition serve different purposes.  You can
combine them into one statement for syntactic convenience, much as you
can in C/C++, but nevertheless both functions still exist and are
explicitly expressed.

In Python, you almost always only need the definition.



More information about the Python-list mailing list