Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '__name__': 0.07; 'main()': 0.07; 'stops': 0.07; 'python': 0.09; "'w')": 0.09; 'explanation': 0.09; 'script,': 0.09; 'wrong,': 0.09; 'bug': 0.10; 'def': 0.10; '2.7': 0.13; 'to:name:python-list': 0.15; "'__main__':": 0.16; 'expected,': 0.16; 'main():': 0.16; 'refactored': 0.16; 'somehow,': 0.16; 'module': 0.19; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'wrote': 0.26; 'message-id:@mail.gmail.com': 0.27; 'declared': 0.29; 'received:209.85.210.174': 0.30; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'subject:?': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; "didn't": 0.36; 'level': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'below:': 0.71; 'andrea': 0.84; 'shocking': 0.84; 'working,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=HKHQGqHl52T93HtlkPNEC5jCyZpDUgCT8NBbdlvloPc=; b=ibc26O6lZkIAt0eknnaNnrFQy0v5mw3PK04DpJzFUUXAq8Y1GZDRquAr2PK/TpGzGq ISqiMsTRHxSA4qxzWMG1Ll4IJ5pObTisUHsNcaq+l2FZCXmRMuOjKQpAyIkGhBPxvTSm TuHHwkOC7qHFPbZd8O135Gs/T/shsExdN9nNXGtU7nUOE/T7H0aw3zIzV5maRVmURwrV iev4nUafy5xuGYdKdtUiUeChhGwcm44ugiIym71dKfBnk91g43YVqyzdObmIlVcyeYYZ GIsya6f2P63xN25wO31RRQa8BP/uPlHI/H4M0Og11gQX2zP9uUkE4q1aiLnWxfn/B79J vJtQ== MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 30 Nov 2012 11:20:15 +0000 Subject: Re: amazing scope? From: andrea crotti To: python-list Content-Type: text/plain; charset=ISO-8859-1 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354274417 news.xs4all.nl 6868 [2001:888:2000:d::a6]:35321 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34094 2012/11/30 andrea crotti : > I wrote a script, refactored it and then introducing a bug as below: > > def record_things(): > out.write("Hello world") > > if __name__ == '__main__': > with open('output', 'w') as out: > record_things() > > > but the shocking thing is that it didn't actually stopped working, it > still works perfectly! > > What my explanation might be is that the "out" is declared at module > level somehow, > but that's not really intuitive and looks wrong, and works both on > Python 2.7 and 3.2.. Already changing it to: def record_things(): out.write("Hello world") def main(): with open('output', 'w') as out: record_things() if __name__ == '__main__': main() makes it stops working as expected, so it's really just a corner case of using the if __name__ == '__main__' which I had never encountered before..