Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'python,': 0.02; 'subject:Python': 0.05; 'failing': 0.05; '21,': 0.07; 'function,': 0.07; 'javascript,': 0.07; 'python': 0.09; 'braces': 0.09; 'rules.': 0.09; 'abusing': 0.16; 'braces,': 0.16; 'curly': 0.16; 'earlier.': 0.16; 'editor,': 0.16; 'keyword.': 0.16; 'overwriting': 0.16; 'scope,': 0.16; 'scope.': 0.16; 'scopes': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'variables': 0.17; 'feb': 0.19; 'variable': 0.20; 'file.': 0.20; 'earlier': 0.21; 'facility': 0.22; "python's": 0.23; "haven't": 0.23; 'header:In- Reply-To:1': 0.25; 'possibly': 0.27; 'see,': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'received:209.85.212': 0.28; 'all.': 0.28; '(unless': 0.29; 'accidentally': 0.29; 'declared': 0.29; 'probably': 0.29; 'function': 0.30; 'code': 0.31; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'too': 0.36; 'one,': 0.37; 'does': 0.37; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'here:': 0.62; 'details': 0.63; 'more': 0.63; 'within': 0.64; 'sound': 0.65; '2013': 0.84; 'to:name:python': 0.84; 'hand,': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type:content-transfer-encoding; bh=Ip9rvek7+yndh5lZU93/mIBP/rbCzEjQKDPAvKFS5/U=; b=j9Z+gQGyAtvUFyvKCItUcdOgp60/kxfXsyQfFudRPpHHd1g4ExCiSAsuOemFKKroai EcHNjp0FIhFaDSGS1qhssS294P60pcaZS6Ec15kRymDd11vcP5kQAyGfp4F1I7w6HvPD fbtto75276hz7f4L6oWNZVZ7xVM1iwy8xAdLPQ7kMCIsMJIWOllEw4TFtDEANfN4HaK4 zBHfcTFxX5d7HZDZul7FQtEqfiY+iwYIg1GAZQZsVzQDZZaWZKFTGud+qxXmCweCaxaa KFzxo294dGJv6GoJ2+5ir/gWIcTlsrPbqBzBVtCsdGYedEuCjfAvLNkyouT7JxrcdFSM qGyA== X-Received: by 10.58.18.201 with SMTP id y9mr34911147ved.36.1361490984427; Thu, 21 Feb 2013 15:56:24 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <5c262e95-b3a8-4f2a-b752-84b30bf4f81e@googlegroups.com> References: <5c262e95-b3a8-4f2a-b752-84b30bf4f81e@googlegroups.com> From: Ian Kelly Date: Thu, 21 Feb 2013 16:55:44 -0700 Subject: Re: Python Newbie To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361491414 news.xs4all.nl 6901 [2001:888:2000:d::a6]:43726 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39497 On Thu, Feb 21, 2013 at 3:40 PM, wrote: > I am nervous about using variables "out of the blue", without having to d= eclare them. For example, when I write "i =3D 0" it is perfectly OK to Pyth= on without 'i' being declared earlier. How do I know that I haven't used th= is 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 fu= n. If you need to search for variable names to see if you're overwriting something, then your functions are too large and should probably be refactored, or you're abusing globals, or possibly you just haven't fully understood Python's scoping rules. > 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 ju= st used is limited in scope to the code within the { }. With Python, you ha= ve to search the whole file. Er, this doesn't sound right at all. Javascript does have variable declarations, using the "var" keyword. Within a function, a declared variable has local scope, but an undeclared variable has global scope. Despite having curly braces, Javascript does not have block scopes like C# does. More details can be found here: http://stackoverflow.com/questions/500431/javascript-variable-scope In Python, on the other hand, an undeclared variable in a function is local by default (unless it is never assigned to). To me, this makes Python win out over Javascript because you're never going to accidentally create a global variable just by failing to declare the variable in the function where you use it.