amazing scope?

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Nov 30 20:21:55 CET 2012


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..

Makes sense to me. `out` is declared in an if statement. If statements
have no "scope" and it is not in a function so it gets added to the 
module's namespace.


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list