Python Newbie

J.R. groups_jr-1 at yahoo.com.br
Sun Feb 24 21:02:15 EST 2013


On 21/02/2013 19:40, piterrr.dolinski at gmail.com wrote:

<snip>

> I am nervous about using variables "out of the blue", without having
> to declare them. For example, when I write "i = 0" it is perfectly OK
> to Python without 'i' being declared earlier. How do I know that I
> haven't used this variable earlier and I am unintentionally
> overwriting the value? I find I constantly have to use the search
> facility in the editor, which is not fun.
>
> You see, Javascript, for one, behaves the same way as Python (no
> variable declaration) but JS has curly braces and you know the
> variable you have just used is limited in scope to the code within
> the { }. With Python, you have to search the whole file.
>

No, JavaScript - or better, ECMAScript -, does have variable 
declaration. If you don't declare a variable with the "var" statement it 
will become an unintentional global variable which is a very bad thing.

If the variable statement occurs inside a Function Declaration, the 
variables are defined with function-local scope in that function. 
Otherwise, they are defined with global scope (that is, they
are created as members of the global object. Variables are created when 
the execution scope is entered. A Block { } does not define a new 
execution scope.

PS.: JavaScript is a trademark, and the actual language name is 
specified as ECMAScript.

See <http://ecma-international.org/ecma-262/5.1/>

-- 
Joao Rodrigues (J.R.)



More information about the Python-list mailing list