Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You are mistaken. try...except does not introduce a scope in Python. Only "def" and "class" do.


Yup.

    try:
        x = uncertain_op()
    except ...:
        x = 'fallback'
    do_something(x)
is a common Python idiom; no initial definition required. Although this isn't true in every language.


Hm, I suppose you're right. I just tested it. I can't for the life of me remember what I was thinking of then.


In many (most?) other languages compounds / blocks always introduce a scope. E.g. in C++ (or Java)

    try { /* new scope */ }
    catch (FooException &foo) { /* another scope */ }
Python is probably one of not that many languages where this is not the case.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: