News for hackers. Stuff that matters.
Re:I don't like Python. Does that make me a bad person? by HarrySimons on Saturday June 26, 2010 @06:40AM (#1463451)
I hate Python too, only because of its insistence on indentation and because of its verboseness relative to, say, Perl.

I'm not against the non-Perl philosophy of There Is Only One Way To Do It, but for God's sake, don't insist on indentation! I am under a personal vow that until Python lifts its indentation requirement (say, via a command-line switch), I'm no way using this language for my programming needs.

Re:I don't like Python. Does that make me a bad person? by jsankey on Saturday June 26, 2010 @06:52AM (#1463465)
I've never understood this objection. Do you not indent your code anyway? If so, why should you also need to delineate scopes in another way? Python allows you to choose your indentation, it just requires consistency: something that is always required by coding standards (with good reason).

If there was a command line switch, would you use it? Why?

Re:I don't like Python. Does that make me a bad person? by cageface on Saturday June 26, 2010 @10:16AM (#1463694)
Python's whitespace indentation seems like a great idea until you realize that it makes things like copying & pasting code from web pages etc far more difficult and, worse, makes automatically reindenting a block of code in an editor impossible. If I come across a badly formatted block of code in Ruby or Java or C in emacs it takes me one keypress to sort it out. In Python I have to carefully, manually look at each line.

It's also fun to spend an hour looking for a bug caused by somebody inserting a tab instead of a space somewhere. We had a production crash that took us a long time to fix because somebody hotfixed a file with the wrong editor and did this.

I also don't understand how forcing people to indent code in the same way magically makes people write good code. The kind of programmer that can't or won't indent his code properly is introducing bugs far more serious than bad layout into your project.

Python's a nice language in many ways but I consider the syntax to be a bug.

Re:I don't like Python. Does that make me a bad person? by Jeema3000 on Saturday June 26, 2010 @11:07AM (#1463760)
It's also very annoying to have to un-indent an entire block of code during debugging just because you want to comment out the conditional statement around it...
Re:I don't like Python. Does that make me a bad person? by ableal on Saturday June 26, 2010 @12:40PM (#1463888)
You could do what you say - comment the conditional:

    # if x:
    if 1:         # DEBUG
        block
Re:I don't like Python. Does that make me a bad person? by pjscott on Saturday June 26, 2010 @03:22PM (#1464132)
Or, even quicker and dirtier:

    if 1: # x:
        block
Re:I don't like Python. Does that make me a bad person? by prodigal_erik on Saturday June 26, 2010 @09:21PM (#1464659)
> it makes things like copying & pasting code from web pages etc far more difficult

This. I'm convinced half the reason Ruby is displacing Python is that it's actually practical to talk about Ruby code in forums without ridiculous little-known workarounds like pastebins.

Re:I don't like Python. Does that make me a bad person? by cageface on Sunday June 27, 2010 @05:22PM (#1466256)
I do think that it's a pretty major issue, although I also don't get the impression that Ruby is displacing Python. If anything it seems like the momentum from Rails is petering out a bit.

The Ruby community itself does strike me as more energetic though. All the activity on github etc seems a lot more impassioned to me than the coolheaded & practical world of Django et al.

Re:I don't like Python. Does that make me a bad person? by lars512 on Saturday June 26, 2010 @10:43AM (#1463735)
I consider the indentation approach excellent because it's so clear and readable. For exactly the same reason, I prefer YAML over XML, because the result is something more readable, something whose structure is understandable at a glance.
Re:I don't like Python. Does that make me a bad person? by sbov on Saturday June 26, 2010 @01:40PM (#1463971)
I'm willing to forgive the creators of Python if they didn't optimize their syntax for copying-and-pasting code.

Hotfixing is a naturally perilous process regardless of language. I've spent hours tracking down untested hotfixes in Java.

You're also probably less likely to come across a badly formatted block of python code because that would generally result in a bug. Much like misplaced brackets in Java/C/C++ would result in a bug.

It doesn't make the code magically better. It makes it less redundant. Indentation should already be there, why do you need curly brackets?

Re:I don't like Python. Does that make me a bad person? by cageface on Saturday June 26, 2010 @05:41PM (#1464331)
I'm willing to forgive the creators of Python if they didn't optimize their syntax for copying-and-pasting code.

Copying and pasting is also known as refactoring. A syntax that makes that much harder than it needs to be is mis-optimized, IMO.

Re:I don't like Python. Does that make me a bad person? by Jach on Saturday June 26, 2010 @07:05PM (#1464469)
But code is read far more than refactored, so I think it's an okay tradeoff. (Plus what editors are ya'll using where changing the indentation of something is difficult?)
Re:I don't like Python. Does that make me a bad person? by alphamerik on Saturday June 26, 2010 @08:20AM (#1463575)
I do indent my code, but I prefer to auto-indent. Using indentation to determine if a line of code falls inside or outside some logic block requires a lot more effort for me. Would much rather just use curly braces. Curly braces also make copying and pasting code blocks much easier.
Re:I don't like Python. Does that make me a bad person? by nailer on Saturday June 26, 2010 @08:34AM (#1463591)
I use auto indent on python all day. Not having to perform secondary bracket mark up is great.
Re:I don't like Python. Does that make me a bad person? by maw on Saturday June 26, 2010 @10:03AM (#1463679)
You don't, because auto indent for python doesn't and can't exist.
Re:I don't like Python. Does that make me a bad person? by crystalis on Saturday June 26, 2010 @10:42AM (#1463733)
You say this, and get points, but I'm not sure what you mean. I've written Python and gotten each new line to automatically indent to the proper point, with a functional backspace that unindents the appropriate amount. I've then copy-pasted blocks with several different indentation levels that were automatically adjusted to the surrounding code, with the possible exception of an error corrected by a single keypress.

To my knowledge, this is no worse than the expectation for a braced language, but much better than the worst case for a braced language.

If you mean something simpler than I've taken you to mean, e.g., the conversion of a whitespaceless C program into a whitespaced one, I concede you are mostly correct (';' still works in Python), but I must admit I would still not yet see your point.

Re:I don't like Python. Does that make me a bad person? by maw on Sunday June 27, 2010 @09:42AM (#1465468)
If you write code like this:

  if x:
      # do stuff
  # do other stuff
Your indenter will not know where to put the third line.

In languages whose blocks are closed using nonwhitespace the problem doesn't exist. Your indenter just has to push everything as far to the right as it can, and problem solved.

It's nice that your editor gets copy and paste right, but it doesn't and indeed can't decide how to indent your code, something that works in every other language I've dealt with.

Re:I don't like Python. Does that make me a bad person? by crystalis on Sunday June 27, 2010 @08:24PM (#1466608)
Why does that matter?

In brace language:

if (condition) {{ code_1 }} code_2

In Python:

if (condition):\n code_1 \n^H code_2

Unless your editor also knows when to place your close braces, and, haha, it doesn't and indeed can't, you have not even won a keystroke.

Re:I don't like Python. Does that make me a bad person? by maw on Tuesday June 29, 2010 @03:52PM (#1472546)
Once I've placed my closing braces, I'll never need to think about it again, unless and until I restructure that piece of code.

It's got nothing about saving keystrokes.

And now, back to working on pass-infested Python code.

Re:I don't like Python. Does that make me a bad person? by koenigdavidmj on Saturday June 26, 2010 @01:38PM (#1463969)
Vim does pretty well. On starts of blocks (when I hit : followed by return) I get an indent. At the usual places that would end a block (return, raising an exception, break or continue) I get a dedent automatically.
Re:I don't like Python. Does that make me a bad person? by gcr on Saturday June 26, 2010 @11:05AM (#1463757)
Are you sure? A counterexample: My editor indents every line that I end with a colon (:). So I just type : and <enter> and have a new indent. When I want to unindent, I just type backspace to go back one tab. This is simple to implement and understand, and with python, it's really all I need.

This kind of "automatic indentation" (colon-enter to indent, a quick backspace to unindent) feels much more natural to me than a C-style indent, where the editor's cue to indent would be space-rightbracket-enter and leftbracket-enter to unindent.

True, one cannot come accross a completely unindented block of python code and ask the editor to format it correctly for them. But by definition, unindented code is not valid python and will not run. This is not unlike C / C++ / Java code that's missing all its braces, which would not run either.

Re:I don't like Python. Does that make me a bad person? by jonknee on Saturday June 26, 2010 @10:43AM (#1463737)
I think he means in his editor. I do the same, it works well.
Re:I don't like Python. Does that make me a bad person? by T_S_ on Saturday June 26, 2010 @11:36AM (#1463793)
Whitespace doesn't help you write code. I helps you read code, which is a lot harder writing it, even though it seems like it should be the other way around.

The pain of keeping indentation correct is a forced ongoing investment in readability that is well worth it--at least to me.

I'm glad that haskell also has whitespace rules.

Re:I don't like Python. Does that make me a bad person? by melling on Saturday June 26, 2010 @08:13AM (#1463566)
I indent my Perl code. Not everyone does, however. It's a trivial task to reformat it in an editor. I guess the only complaint I might have is why bother thinking about indentation, ever? Throw in a couple of {} and now the tools can provide additional help.
Re:I don't like Python. Does that make me a bad person? by edanm on Saturday June 26, 2010 @08:53AM (#1463612)
The point is, I've never met a programmer who doesn't think of indentation. I've never seen a (real) project which doesn't have indentation. So if everyone is doing it anyway, why not take advantage of it and make it a part of the language? This way programmers learn to use indentation from the start (which any real programmer uses anyway), and indentation is always correct, nothing to ever think about.
Re:I don't like Python. Does that make me a bad person? by util on Saturday June 26, 2010 @10:40AM (#1463731)
It makes it harder to correctly do operations on code as text, eg, inserting a chunk of code from elsewhere or moving it out from or under an "if" statement. I have trouble with this occasionally working in emacs at least.
Re:I don't like Python. Does that make me a bad person? by edanm on Sunday June 27, 2010 @02:36AM (#1465091)
This does get to be a problem with people not indenting consistently. I.e., some code using tabs and some using spaces, and then copying between them.

Technically, your editor should take care of that for you, but some of them don't do it properly (and sometimes can't do it properly), and it really sucks.

Well, everything has a downside :)

Re:I don't like Python. Does that make me a bad person? by melling on Saturday June 26, 2010 @02:37PM (#1464064)
Shouldn't tools take care of that before a commit to svn, git, etc? Java has formatters, the Go language has one.

Do any exist for Python or Perl?

Re:I don't like Python. Does that make me a bad person? by edanm on Sunday June 27, 2010 @02:35AM (#1465089)
Take care of indentation, you mean?

1. People don't indent consistently. There are a lot of (imo, pointless) arguments about where to place the braces, etc. One of the advantages of the language forcing a style is that arguments about it are no longer necessary.

2. Why should tools be taking care of this? People indent to understand the code better. Everyone agrees that you have to do it. So what's the problem with the language forcing you to do it? I'd rather it be part of the language than an external tool enforcing it.

Re:I don't like Python. Does that make me a bad person? by jsankey on Saturday June 26, 2010 @09:02AM (#1463617)
I indent my Perl code. Not everyone does, however. It's a trivial task to reformat it in an editor.

But in Python, you don't even have to worry about other people not indenting their code!

I guess the only complaint I might have is why bother thinking about indentation, ever? Throw in a couple of {} and now the tools can provide additional help.

When you edit Python, the tools can already help as much. The start of a block is obvious (def, if, for, etc), so tools indent for you. To dedent, just hit the right key (e.g. a backspace on an empty line in emacs) -- no harder than hitting the brace key.

Re:I don't like Python. Does that make me a bad person? by dmn001 on Saturday June 26, 2010 @11:54AM (#1463821)
You do have to worry as their Python code produces errors if they do not indent it properly!

I don't like Python either and prefer Perl. (In Python I did not like indentations, dicts, tuples, lists, and the IDLE gui.)

Re:I don't like Python. Does that make me a bad person? by Jach on Saturday June 26, 2010 @07:01AM (#1463480)
First, to the topic question, I'm not qualified by your criteria to answer the question, but I'll say anyway that I prefer doing video game programming in Python for 99% of the tasks that come up compared to C++. (For example, the PyGame wrapper for SDL just feels a lot better than the C++ interface, plus it adds some nice features.) My own take on mental models can be simplified with the example of strings. In Python, I can think of a string as a string, or a word, or a sentence, or an iterable list of characters. In C, I can't ever forget that it's all pointers. Python lets me think at higher abstractions more readily from looking at code. Plus I really love dynamic duck typing.

And to the parent: I really hate to be "that guy", but can you give me an example where non-indented code looks better than indented code (since all your complaint really is about is style rather than what Python can help you do)? If it's just braces you're after, Python "supports" those by just commenting them out. When I first learned Python years back I was coming from a PHP and some Java background, and my code was sometimes single-space-indented, sometimes not indented at all, and it was hideous. After I realized I should be indenting anyway, Python made a lot of sense, all these languages with braces or 'end's everywhere didn't, and I can't think of any case I've ever thought "Man, I wish I didn't have to indent here." The one thing I can say I don't exactly like about Python now is the occasional self-hell.

Re:I don't like Python. Does that make me a bad person? by bad_user on Saturday June 26, 2010 @07:14AM (#1463494)
Sometimes I have trouble when breaking looong lines of Python code in multiple lines ... like for example when I have method chaining. I don't like long lines of code, it makes the code unreadable.

Of course, most situations can be solved by a backslash-enter, and simple statements can be separated by ; but there are places where I wished for more flexibility.

This is only about readability, and overall Python code tends to be more readable than in other languages, although I've read beautifully written code in Java or Perl or C++, and the readability really depends a lot on the programmer writing it.

I think the major problem is the lack of focus in high-school / college on such things ... quite the contrary, many teachers are usually disconnected from real-world projects and don't have this skill themselves.

Re:I don't like Python. Does that make me a bad person? by birken on Saturday June 26, 2010 @07:23AM (#1463512)
Google python style guide has a very good way of dealing with long lines:

http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...

In fact at Google the maximum line length for python code is 80 characters because long lines make the code unreadable, and following the style guide basically anything can fit in a nice and readable way.

Re:I don't like Python. Does that make me a bad person? by cookiecaper on Saturday June 26, 2010 @07:16AM (#1463499)
The indentation is not a big deal at all. You indent your code anyway, right? Python doesn't enforce a specific standard, you can indent however you want, just as long as you use the same units of indentation consistently.

The only time I've had a problem with indentation is while copy-pasting things, occasionally that can get messed up and be annoying. Everything else is fine.

Re:I don't like Python. Does that make me a bad person? by RodgerTheGreat on Saturday June 26, 2010 @09:31AM (#1463647)
The main reason I like having explicit braces and non-significant whitespace is that it allows me to use my whitespace flexibly to clarify the structure of a program.

There are some interesting ideas regarding the use of whitespace in C here:

http://www.quinapalus.com/coding.html

I don't agree with everything he has to say- for example, I think his nested for loop example would be far nicer as something like this:

  for(i=0;i<10;i++) {
  for(j=0;j<10;j++) {
  for(k=0;k<10;k++) {
  	printf("%d %d %d\n",i,j,k);
  }
  }
  }
(Not to mention more in keeping with the principle of "alignment".) Still, if nonstandard indentation can make it easier to see the patterns in your code, it benefits both development and maintenance.
Re:I don't like Python. Does that make me a bad person? by thedigitalengel on Saturday June 26, 2010 @09:51AM (#1463665)
Would it not be better this way:

for (i = 0; i < 1000; i++)

     printf ("%d %d %d\n", i / 100, (i % 100) / 10, i % 10);
IMHO, such patterns (multiple nesting with empty outer loops), in most cases, are really an indication of just one real loop. Using multiple counters might also be an option.

In case the loops are doing something different, they should be indented separately. For instance, the following makes little sense:

for (i = 0; i < 10; i++) {

foo (i);

for (j = 0; j < 10; j++) {

     foo (i + j);
for (k = 0; k < 10; k++) {

foo (i + j + k);

}

}

}

Re:I don't like Python. Does that make me a bad person? by RodgerTheGreat on Saturday June 26, 2010 @10:25AM (#1463707)
I absolutely agree with your second example, but I think you're missing my point for nested loops. Perhaps this would be a better example:

Let's say I'm making a simple tile-based game that uses a two-dimensional array called "grid" to store the map. To draw that map to the screen, I might do something like the following:

  for(int x = 0; x < grid.length;    x++) {
  for(int y = 0; y < grid[0].length; y++) {
  	drawTile(grid[x][y], x * tileWidth, y * tileHeight);
  }
  }
There are other ways to refashion this as a single loop, (storing everything in a 1d array and handling the dimensioning ourselves, using an iterator and giving tiles a position attribute containing their (x,y) coordinates, etc) but is any of that really more straightforward?

Again, I am not arguing for indentation anarchy, merely that there are times when it can help to bend the rules.

edit: I imagine you'd prefer something like this?

  for(int t = 0; t < grid.length * grid[0].length; t++) {
  	int x = t % grid.length;
  	int y = t / grid.length;
  	drawTile(grid[x][y], x * tileWidth, y * tileHeight);
  }
Re:I don't like Python. Does that make me a bad person? by dnorris10 on Saturday June 26, 2010 @07:08AM (#1463489)
The only reason I have had lately to get annoyed at Python is when one of the guys I'm working with on a project was using an editor where the tabspacing wasn't set correctly. That meant anything that he changed/added led to the script not working. Even though this was an easy change (=G in vim), it's still annoying to think that such a minor issue would completely kill the script.

Of course I prefer Ruby, but that's a whole other can of worms :)

Re:I don't like Python. Does that make me a bad person? by gaius on Saturday June 26, 2010 @07:26AM (#1463515)
In C and C-like languages, you have indentation for the humans and curly brackets for the compiler. Python simply unifies this into a single convention. Both parties "see" the code in exactly the same way. If you see someone else's code, there's never any ambiguity about what they meant.
Re:I don't like Python. Does that make me a bad person? by T_S_ on Saturday June 26, 2010 @11:39AM (#1463801)
I'm glad I don't have to read your code. But I don't believe you should be downvoted. Only reformed. ;-)

BTW in haskell whitespace is optional but nobody write code that way.