Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34094 > unrolled thread

Re: amazing scope?

Started byandrea crotti <andrea.crotti.0@gmail.com>
First post2012-11-30 11:20 +0000
Last post2012-11-30 11:20 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: amazing scope? andrea crotti <andrea.crotti.0@gmail.com> - 2012-11-30 11:20 +0000

#34094 — Re: amazing scope?

Fromandrea crotti <andrea.crotti.0@gmail.com>
Date2012-11-30 11:20 +0000
SubjectRe: amazing scope?
Message-ID<mailman.381.1354274417.29569.python-list@python.org>
2012/11/30 andrea crotti <andrea.crotti.0@gmail.com>:
> 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..

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web