4 ms·
One nice thing about significant indentation is that it forces new programmers to learn how to indent their code properly. I'm taking an Intro to Java course no
by LeafStorm 15y ago
One nice thing about significant indentation is that it forces new programmers to learn how to indent their code properly. I'm taking an Intro to Java course now (in retrospect, I should have tried to place out), and most of the students in the class have absolutely terrible indentation.
The only major downside of significant indentation is that it makes it impossible to define multi-line anonymous functions in a clean way.
- Jach 15y agoI totally agree with you on indentation. I started with PHP and didn't bother indenting at all for a long time, then when I did it was only a single space. I tried three spaces for a while in an AP CS course in high school (I also tried and hated Python for a weekend during that first semester), then toward the end of that year I learned Python more seriously and fell in love. I still only indent two spaces though, my One True Way. =P I think part of my initial dislike for Python was manually typing nested indentation, even with one space the level for class->def->for->if can get annoying to type. I used to use EditPad Lite that didn't have auto-indent. Everything got better once I learned vim. Regarding multi-line lambdas in Python, you can do it more or less but like you said it's not clean and can be dangerous. a = [1] blah = lambda x: eval(compile(''' print x a.append(x) ''', '', 'exec'))
- anamax 15y agoPython's lambdas aren't functions, they're single expressions, so I don't think that the multi-line problem is significant. I'm not convinced that multi-statement anonymous functions are a good idea.
- rogerdpack 15y agoThey can make for pretty statements in Ruby. But my question is then...why can't you do it in python just like ruby def go : this.some_other_method : here_is_my_block ?
- gbog 15y ago> multi-line anonymous functions This is maybe a bit far from the topic, but I would like to know why there is such a need for anonymous functions in Python. I understand very well the need for stateless functions (functions that do not change anything else than their return value, have no side effect, except maybe logging), and I understood it was a piece of functional programming. In Javascript, with silent overwriting of functions and without modules, it is obviously very bad to pollute the only namespace with function names that can clash with each other, but in Python, you are or should be always inside a relatively small scope, and there you can def _my_temp_func() without any problems, no? Moreover, as it is lame to use _temp as function name, it forces you to formulate a meaningful function name, which is good pratice, especially for your lectorate, right? Or am I missing something?
- burgerbrain 15y agoI don't know python, so I don't know if anonymous functions have been crippled there, but in other languages anonymous functions are used to do things such as create closures (not stateless). For example, in scheme: > (define (foo x) (lambda (y) (+ x y))) > (define bar (foo 10)) > (bar 5) 15 This sort of thing is of course immensely useful.
- andrewaylett 15y agoYou can do the same in Python, with named functions or lambdas: >>> def foo(x): ... def add(y): ... return x + y ... return add ... >>> bar = foo(10) >>> bar(5) 15
- masklinn 15y agoAnd it takes twice the token count and lowered readability for no gain whatsoever.
- burgerbrain 15y agoNo gain at all? How do you figure?