3 ms·
Add at least two spaces at the beginning of each line and it will be treated as a preformatted block. What you try to do is not legal Python. The else clause
by euccastro 16y ago
Add at least two spaces at the beginning of each line and it will be treated as a preformatted block.
What you try to do is not legal Python. The else clause can't go in the same line.
I get your point, though. Python's scope is not in terms of literally 'indentation' blocks. Scope units are: module toplevel, and (possibly nested) function and class definitions. To create a new scope you create a new function. This is what Lisp does too (you can define let as a macro over lambda).
I have oscillated between liking either way better and at present I find Python's sloppier way more practical.
- sausagefeet 16y agoFixed, thank you for the tip. Yeah the point I was showing was that the 'c' exists after the if or else, showing that scope is not bound to indentation level. On the subject of scope, this behavior has gotten me a few times (I find it unintuitive although I understand why it works this way): x = 2 f = lambda : x print f() x = 3 print f()
- euccastro 16y agoYes, the first assignment works as a Scheme 'define', and all subsequent ones as 'set!'s. The whole function/class/module is a monolithic scope, inherited/superseded/extended by functions and classes defined within.