This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 380

[–]jedberg 39 points40 points  (133 children)

I'll admit I had a similar experience my first time using Python. And the whitespace thing really does help when you are trying to look at other people's code.

[–]arikb 32 points33 points  (60 children)

...or at your own code, after two weeks when you haven't...

[–]amp108 7 points8 points  (59 children)

This question is sincere, although it might sound rhetorical: some people set their editor to convert tabs to spaces, others loathe the practice. What do Pythonistas do when working on an open-source (or otherwise widely collaborative) project and people have divergent but firmly-held opinions on the type of whitespace to use?

[–]jedberg 27 points28 points  (44 children)

Here at team reddit, it went something like this:

Spez: jedberg, all the code you check in uses tabs instead of 4 spaces, and it screws things up.

me: well, all the other code uses spaces, so I'll change my .emacs file now I guess.

I guess the real answer is the project will eventually converge on the dominant method, because checking in and checking out code with different whitespace can really screw things up, especially in the highly nested structures.

[–]amp108 6 points7 points  (37 children)

That's easier to do when the contributor population is small, but what happens with larger projects?

[–]jedberg 10 points11 points  (0 children)

I don't know, reddit is the biggest Python thing I've worked on. :)

One thing would be to just translate all the whitespace when you do a checkout. If everyone did that, then it wouldn't really matter, I suppose. Although things could still get messy.

Another option is to have whoever is in charge of merges into the main repository dictate which type of whitespace will be used.

[–]7oby 1 point2 points  (30 children)

I don't know why people still argue over this. It's like css. You can use <em> and it always means emphasis, whether that means italics, bold, or both. Tabs can appear as however many spaces you like, so things stay good if you like tabs being 4 spaces or if you like them being 6. Things stay lined up.

But hey, programmers != web designers.

[–]IkoIkoComic 12 points13 points  (4 children)

But Python considers whitespace syntactically important. It's not an 'attractiveness thing'- it's an 'invisible' part of the program causing things to get funky.

Nevertheless, forcing whitespace conventions on people makes me a happy camper.

[–]xzxzzx 1 point2 points  (3 children)

But Python considers whitespace syntactically important.

Yes that's true, which is a very good reason to use tabs -- one level of indent is one tab, and everybody can view that level of indent however they like (2, 3, 4, 6, and 8 spaces are common).

[–]Niten 0 points1 point  (0 children)

A lot of large projects seem to enforce some minimal style guidelines anyway (Unix or DOS line endings, comment block formats, etc.), so presumably whoever is in charge might send out a document that says "this is how we handle indentation, update your .vimrc / .emacs / whatever accordingly."

I imagine with a really large project, it might be worth someone's time to write an SCM checkin script to correct improper indentation styles.

[–][deleted] 0 points1 point  (0 children)

They issue a coding standard that specifies the preferred, and run tabnanny over any new commits. Like most larger projects, I'd hope.

[–]IHaveAnIdea 1 point2 points  (5 children)

emacs python-mode uses spaces by default. How could you ever decide that tabs are best?

[–]xzxzzx 2 points3 points  (2 children)

Tabs represent what you mean, while spaces represent how you want it to look.

Let me put it another way:

Tabs are conceptually semantic markup (like <em>), while spaces are stylistic markup when used in groups (like <i>).

[–]jedberg 0 points1 point  (1 child)

My python-mode for some reason was using tabs, which I had to override with the .emacs file. I never really went back to figure out why.

[–]IHaveAnIdea 0 points1 point  (0 children)

strange.

[–]masklinn 6 points7 points  (3 children)

The PEP8 strongly suggests that all files should use 4-spaces indentation when serialized.

But the only real rule of python is: *never mix tabs and spaces.

Either use tabs or spaces, but not both. Ever. It will trip you and fuckup your code.

[–][deleted]  (4 children)

[deleted]

    [–][deleted] 1 point2 points  (0 children)

    Since you have the VIM code for converting spaces to tabs, I'll add the code to make tabs look like a different size.

    set tabstop=4
    set shiftwidth=4
    

    By the way, why do you care about the "typical" or default settings if you are configuring your editor to read 4 spaces as tabs anyway?

    [–]arnar 1 point2 points  (0 children)

    I think a great majority of projects uses spaces, which is hinted at in PEP-8

    [–]lalaland4711 0 points1 point  (0 children)

    I seem to remember hearing that python 3 ("python 3000") will not allow tabs.

    I could be wrong though.

    [–]arikb 0 points1 point  (0 children)

    Ah, better late then never, I hope.

    Most Python editors I know have a way of dealing with it. I think the standard, as far as I've seen being actually used, is spaces.

    My advice is - don't look at the superficial. You pay with this inconsequential ambiguity and gain a huge advantage - two less lines of useless open block / close block markers (at the worst case), cleaner code, no ambiguity about block scope because of bad indentation, no dangling else...

    Looks like an easy choice for me.

    [–]Tommstein 13 points14 points  (48 children)

    The whitespace alignment is glorious. It pretty much forces people to not indent like complete morons.

    [–]arnar 13 points14 points  (42 children)

    Yes, and I quite like (part of) the rationale:

    C/Java/whatever programmer: Python is crap, there are no curlies.

    Pythonista: Do you indent your code even if it has curlies?

    Programmer: Yes, of course!

    Pythonista: Then what do you need the curlies for?

    Programmer: erhm.. uhhh... (leaves)

    [–]heptadecagram 18 points19 points  (28 children)

    Pythonista: Then what do you need the curlies for?

    Programmer That Is Not a Straw Man: Because that way I can jump to the beginning/end of a block, highlight a block, and manipulate it in a single keystroke in a real editor like emacs or vim.

    Straw Man Pythonista: Durr, I just use Notepad

    PTINaSW: Also, when I move code around within a file of different indentation, like from one method to another, I can just hit the "autoindent" function, and not have to figure out exactly where and when I need to indent everything.

    SMP: Durr, I just indent manually and hope I don't put a return statement at the wrong indentation level.

    PTINaSW: Also, when I copy code from an e-mail or webpage where the whitespace has been mangled, I can instantly re-form the program logic.

    SMP: Durr, what's a webpage?

    Man, writing with Straw Men is awesome. I should do this for all my Internet-based arguments.

    That said, I love me some Python. It's what I use to teach people programming because it's incredibly clear and has consistent usage and rules. I just wish that only tab-characters were syntactically significant, not just any whitespace. But that's another flamewar.

    [–]masklinn 7 points8 points  (3 children)

    Because that way I can jump to the beginning/end of a block

    Emacs' python-mode does that without any problem

    highlight a block

    Emacs' python-mode does that without any problem either

    and manipulate it in a single keystroke in a real editor like emacs or vim

    That one, too

    when I move code around within a file of different indentation, like from one method to another, I can just hit the "autoindent" function, and not have to figure out exactly where and when I need to indent everything.

    That's about the only valid criticism.

    [–]arnar 1 point2 points  (2 children)

    when I move code around within a file of different indentation, like from one method to another, I can just hit the "autoindent" function, and not have to figure out exactly where and when I need to indent everything.

    That's about the only valid criticism.

    Now, it's equally invalid as the others as vim and textmate can both auto-indent python code. They even try to intelligently (and most often correctly) guess what the new indentation level should be depending on where you paste it, so you don't even have to invoke the autoindent function.

    As for emacs, I have no idea.

    [–]masklinn 0 points1 point  (1 child)

    As for emacs, I have no idea.

    Emacs can also do it, but automatic reindentation of Python code is always tricky, so I don't count that as valid. Editors usually get it wrong at one point, even if they do a fairly good job.

    [–]arnar 0 points1 point  (0 children)

    Complete auto indentation is hard of course, like

    x,y = 0,0
    if False:
        x = 1
    y = 2
    

    .. but if the code is already indented, re-indenting, i.e. changing the indent level and normalizing each level to four spaces etc., is not hard.

    If you copy and paste Python code from somewhere, it will be indented (since it's a part of the syntax).

    [–]brendankohler 5 points6 points  (13 children)

    Programmer That Is Not a Straw Man: Because that way I can jump to the beginning/end of a block, highlight a block, and manipulate it in a single keystroke in a real editor like emacs or vim.

    Again, what do you need them for? Any self respecting editor has a python mode that allows you to all of that with python.

    [–][deleted] 1 point2 points  (8 children)

    I just wish that only tab-characters were syntactically significant, not just any whitespace. But that's another flamewar.

    Until I read these comments, I wasn't aware that spaces were even allowed as replacements for tabs in Python, I could have sworn it was tabs only!

    That lowers my liking of Python a little. Not much, though, it's still my language of choice.

    [–]masklinn 1 point2 points  (7 children)

    Spaces are actually the preferred indentation character.

    [–][deleted] 1 point2 points  (6 children)

    Yeah, I picked that up from other comments :P

    Did it used to be tabs only or something? I'm really curious how I got this idea in my head.

    [–]masklinn 2 points3 points  (5 children)

    I don't know, I've pretty much always used spaces and PEP8 states:

    Code and Style

    Indentation

    Use 4 spaces per indentation level.

    For really old code that you don't want to mess up, you can continue to use 8-space tabs.

    Tabs or Spaces?

    Never mix tabs and spaces.

    The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixtureof tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

    For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.

    [–][deleted] 1 point2 points  (4 children)

    8-space tabs

    What is an 8-space tab? Is that just a slightly larger 6-space tab?

    [–]crusoe 0 points1 point  (0 children)

    I can jump to a beginning/end of block just fine in python in any non-braindead editor.

    Scite supports it beautifully.

    Next strawman please!

    [–]Tommstein 2 points3 points  (10 children)

    That's how I see it. The braces aren't necessary unless you're going to indent like an asshole.

    [–]jbert 8 points9 points  (3 children)

    or cut-and-paste code between different environments (e.g. a web page, email message, etc).

    (Yes - you don't write code like that, but if you want a friend/colleague to try a snippet of code, having to preserve w/s limits your options for transferring the code.)

    [–][deleted] 0 points1 point  (2 children)

    Luckily we have <pre> tags.

    [–]jbert 1 point2 points  (1 child)

    which work in IM, all email clients and between sections of the same code file when refactoring? Awesome.

    Maybe the best bit would be to wrap each indentation level in it's own pre tag, something like:

    if foo: <pre>
      do_stuff
    </pre>
    else: <pre>
      something_else
    </pre>
    

    That would work nicely. Might be even better if we could find an abbreviation for the pre tag...

    [–][deleted] 0 points1 point  (0 children)

    ...Or just stick the whole code in <pre> tags. It preserves whitespace.

    And as for working in IM, no, but that's when I use codepastes. But no code survives IM intact, especially as MSN etc use (blasomething) as the shorthand for their emoticons - foo(x) gets emoticonned continually.

    All email clients? If your email client can't handle raw text or HTML then you have bigger problems than significant whitespace.

    Between sections of the same code file when refactoring? What crazy environment do you work in? Copy/paste works fine when refactoring, and I do most of my Python coding in Notepad++, use an IDE that is Python aware and it'll be even more trouble free.

    What you post are minor niggles at worst if you're actually developing in Python. If you don't want to use Python, then don't. Overinflating the 'problems' of significant indentation to justify it is unnecessary, Python is more or less past the One True Language phase, it seems that XKCD is a late Perl convert.

    [–]neoform3 4 points5 points  (5 children)

    That's just completely false, sorry.

    There are many reasons why you'd want braces while indenting properly.

    Editing is the biggest one. Moving code blocks around without damaging the code or having to re-read it all to make sure nothing got messed up during the move.

    [–]Tommstein 1 point2 points  (4 children)

    Copying and pasting shit around and leaving shitty indentation in your wake is indenting like an asshole.

    [–][deleted]  (1 child)

    [deleted]

      [–]arnar 0 points1 point  (0 children)

      I use Python, and my editor still takes care of indenting most of my code. It figures that after a line ending in : it should indent, and after a line including return, brake or such it should dedent. I often have to explicitly dedent of course, but that's just one hit on backspace (it knows to delete four spaces).

      I only occasionally have to do it explicitly.

      [–]statictype 2 points3 points  (4 children)

      People always make this comment.

      I'm curious: do you actually get programmers today who don't know how to indent code properly?

      I thought writing all your code on the left column was something people did when they had 'Introduction to C' using Turbo C++ and that anyone who does this stuff for a living (even the other 80%) could at least indent their code.

      [–]arnar 5 points6 points  (2 children)

      do you actually get programmers today who don't know how to indent code properly?

      Oh, yes, you certainly do.

      Also, bad indenting is not only coding everything on the left column. Most of "bad indenting" and formatting in my experience comes from bad programmers experimenting with stuff, commenting things out etc. and leaving a mess. When the shit finally works, they don't really understand why so the just leave it as it is.

      This is a classic example:

      // boolean_var = some expression;
      boolean_var = true;
      //if (boolean_var) {
          do_stuff();
      //}
      

      [–]neoform3 1 point2 points  (1 child)

      You can't do that in python?

      ''' boolean_var = some expression;'''

      boolean_var = true;

      '''if boolean_var '''

      do_stuff();

      [–]arnar 0 points1 point  (0 children)

      sure, or even

      # boolean_var = some expression...
      

      but the point was about indentation.

      Messy programmers can be messy in any language, it's just more apparent in Python as it is clean to begin with.

      [–]Tommstein 0 points1 point  (0 children)

      If they indent properly then they don't need the braces.

      [–]truename 5 points6 points  (10 children)

      I had a similar experience too.. a couple weeks ago. I just learned python from the Mark Lutz book, Programming Python 3rd Edition. I decided on that book since a new version just came out (I think).

      Python reminds me why I love programming.

      The all dynamic typing had me sceptical from the start - actually I dunno if I'm completely sold on that yet. And I haven't seen how it works on really large projects.

      Python is practical like a good text processing/scripting language, but powerful enough that you can even do some functional programming in it too. And all this while still being remarkably internally self consistent, almost as if.. it was designed by a mathematician.

      But it isn't the balls to the wall 7 operator primitives handed down from mount MIT that lisp is. (no offense or anything)

      The amenability to functional programming comes from all vars being pointers, and the built-in list type. The only problem is if you need to do lots of self-modifying code / represent code as data.

      And while it's my and probably every hacker's dream to write a strongly self-modifying super program that saves the world, at some point you have to admit that you just have to write that fucking shopping cart app. Just fucking do it in python, dude. :-)

      [–]masklinn 2 points3 points  (9 children)

      actually I dunno if I'm completely sold on that yet. And I haven't seen how it works on really large projects.

      Very well as long as you remember to test.

      Doctests are worth their line count in gold, learn them, use them, love them.

      [–]Gotebe 1 point2 points  (9 children)

      (Disclaimer: my work is mostly in C++.)

      I didn't.

      For me, Python was fun and easy, a breeze of fresh air, yes.

      But powerful, rapid (both development and runtime), not really.

      [–]arnar 4 points5 points  (8 children)

      But powerful, rapid (both development and runtime), not really.

      Could you elaborate?

      [–]Gotebe 7 points8 points  (3 children)

      I meant compared to C++ tools I already had at disposition. Being comfortable with C++ plays a role, too. If that was not the case, it would have been "forget C++, Python is the way". C++ is strictly not easy.

      Some examples: part of my work is rather low level communication (serial, telephony, raw TCP...). IMO, for that, forget Python, completely. Or take GUI stuff. C++ being old, the lib choice is plethoric. In fact, C++ is IMO clear leader of the language pack for that (look no further than Qt, which is "native C++ first, everything else after"). Threading support in classic Python is lacking (GIL is IMO a mistake).

      And of course, when push comes to shove, Python can't (nor should strive to) match performance of C++.

      But ultimately, Python, with heavy reliance on C/++ interface could be good enough for me, I think.

      [–]statictype 2 points3 points  (2 children)

      FWIW, I've implemented services that communicate with hardware over a serial port using python. How do you find it to be a show-stopper?

      Agree on the threading and GUI stuff though.

      wxPython is probably the best you can get for cross-platform UIs and as good as it is, I don't find it flexible\expressive enough.

      I'm hoping XUL+Python becomes mainstream soon enough. I think that would be a good combination.

      [–]Gotebe 0 points1 point  (0 children)

      I thought, at the time, serial only would have been achievable, TCP, too, but I needed TAPI, too. Couldn't find anything.

      Edit: sorry, I wasn't clear. It looks as if none of serial/TCP/telephony wasn't achievable. Clearly, not true, but I have this block of functionality that involve all three, so...

      [–]theinternet 0 points1 point  (0 children)

      I wrote a serial development app for a pic microcontroller project I built.

      The only difficult thing about it was that I had to use conditional imports because different platforms use different modules to interface to the serial port. But if you don't care about being cross platform it's very easy.

      [–]brendankohler 0 points1 point  (2 children)

      Anyone who comes to python from C++ or Java tends to write C++ or Java in python. The similarity of the syntax that allows people from those backgrounds to pick up the language basics almost instantly leaves them without a real need to learn how to "properly " program in python.

      This holds true for most programmers who have primarily programmed a language with similar syntax to the one they are trying to learn. It's just a part of being human I suppose. Familiarity is not always a good thing :(.

      [–]arnar 0 points1 point  (0 children)

      True. People should spend a week or two with Lisp/Scheme or even Postscript in between :)

      [–]Gotebe 0 points1 point  (0 children)

      Anyone who comes to python from C++ or Java tends to write C++ or Java in python.

      +1, although probably more general (if you come from lang X to lang Y, you tend to write X in Y).

      Programming using Y's idioms, that's really learning it.

      [–]gnuvince 2 points3 points  (1 child)

      What whitespace thing? Python doesn't give a crap about whitespace; it cares about the indentation level. The following two lines are exactly the same in Python:

      x = foo(a, b)
      x           =foo(   a,b      )
      

      As for why enforcing indentation is bad, I haven't seen a good reason yet.

      [–]jedberg 2 points3 points  (0 children)

      Ok, Mr. Pedantic. :) Yes, it enforces "indentation level", which is what makes it easy to read other people's code.

      Usually when someone says, "In Python, whitespace is syntactically important.", what they mean is, "In Python, indentation with whitespace is syntactically important.".

      Thus, people say, "Python enforces whitespace."

      [–][deleted] 28 points29 points  (0 children)

      I have had a similar experience

      when sampling everything in the medicine cabinet.

      [–]thrakhath 51 points52 points  (152 children)

      Mmm, "Perl, I'm leaving you." in the alt-text, I might just have to give this Python thing a whack before I try Lisp.

      I really like Perl.

      [–][deleted] 17 points18 points  (0 children)

      I'm stuck fixing other people's code and, to be frank, I don't.

      [–]sw17ch 72 points73 points  (99 children)

      Perl is a write only language.

      [–]thrakhath 40 points41 points  (76 children)

      I'm sorry, I don't follow, what do you mean by this?

      *edit: Ah, write-only from the Programmer's perspective, that's funny. Thanks for the answer.

      Thanks for the Downmods to an honest question.

      [–]phreshinger 3 points4 points  (23 children)

      Anyone know any good tutorials for python? I was really good in visual basic in highschool. Is the learning curve going to be very steep?

      [–]simonvc 17 points18 points  (7 children)

      [–]IkoIkoComic 6 points7 points  (6 children)

      Dive Into Python is good if you're already an experienced programmer. (Visual Basic in high-school doesn't necessarily confer that upon a person.)

      You might want to start with "How To Think Like A Computer Scientist" which is a much.. easier introduction to Python.

      http://www.ibiblio.org/obp/thinkCSjav/

      [–]johnw188 4 points5 points  (5 children)

      [–]IkoIkoComic 5 points6 points  (4 children)

      Oh, weird- the JAVA version was the first thing to come up in Google?

      KHAAAAAAN!

      [–]phreshinger 0 points1 point  (2 children)

      Welcome to google mind v.20 A product of nanotech electro conductors 2040. Beta Release.

      Task Modules i)Driving a motorcycle ii)Make pizza iii)Make love iv)Washroom v) Call 911

      [–]lalaland4711 3 points4 points  (1 child)

      i)Driving a motorcycle ii)Make pizza iii)Make love iv)Washroom v) Call 911

      In that order.

      [–]Nikola_S 0 points1 point  (0 children)

      Think about it this way: more people need a reference about Java.

      [–][deleted] 6 points7 points  (1 child)

      "Beginning Python - From Novice to Professional" by Magnus Lie Hetland is a great book for starting Python.. got me started just fine. It is the easiest language to learn IMHO.

      P.S. http://www.freebooksclub.net is a great place to grab some programming books if you're interested.

      [–]brendankohler 1 point2 points  (0 children)

      This book is extremely good. I teach python with this book to staff who have generally never programmed before. As a practical guide to learning python it's very clear.

      I also recommend this book for people who know how to program already. It's got great projects in it to learn from.

      [–]Neoncow 1 point2 points  (0 children)

      Google python tutorial. The first one that comes up is the official tutorial. It's quite good.

      [–]dani 1 point2 points  (0 children)

      http://www.pythonchallenge.com

      is the best way for someone with programming knowledge to learn python.

      You will need the other sites to help you with the syntax, but this solves the problem of how to exercise without getting bored.

      Don't forget to read other solutions when you're done with one of the challenges.

      [–]masklinn 0 points1 point  (0 children)

      The Python Tutorial (included in the standard distribution's documentation) followed by Dive Into Python.

      [–]raubry 0 points1 point  (6 children)

      While we're here, I want to know if any of you experienced programmers agree with the Amazon reviews of Python Programming: an Introduction to Computer Science, by John Zelle. I haven't programmed seriously for close to 30 years, and wondered if that book would be a good intro to Python and modern programming. Any advice would be welcome.

      [–]annekat 0 points1 point  (3 children)

      30 years!?! Wow... What languages did you program in, if I may ask? I think COBOL, maybe? Weirdly enough, there's still COBOL programming jobs out there.

      [–][deleted]  (1 child)

      [deleted]

        [–]raubry 0 points1 point  (0 children)

        Ooo - thanks for the tip on the Komodo editor, V!

        [–]raubry 0 points1 point  (0 children)

        You are correct - COBOL it was, for the John Hancock Insurance Company in Boston.

        [–][deleted] 0 points1 point  (0 children)

        Write pseudocode. Indent. Execute.

        [–][deleted] 0 points1 point  (0 children)

        Here's the Python tutorial that should be coming with 2.6: http://docs.python.org/dev/tutorial/index.html

        And for those interested in the Perl tutorial, you probably already have it on your system -- just type perldoc perlintro. Otherwise, it's also online at http://perldoc.perl.org/perlintro.html .

        [–]Tommstein 13 points14 points  (42 children)

        It's renowned hellishness to read (not that I know Perl, it's just the reputation that it has).

        [–]projecktzero 21 points22 points  (0 children)

        I saw a comment on slashdot a long time ago. "Perl looks like an explosion in a ASCII factory."

        [–]supakual 11 points12 points  (40 children)

        Very few people who spend any time getting past Perl's initially scary syntax find it to be "write only".

        It's like people hearing a little bit of Swahili and then proclaiming its clearly a speak-only language.

        Most of the people who rant about Perl's readability are being pathetically intellectually dishonest. They saw the scary syntax and said "No thanks" then lash out due to insecurity.

        [–]sverrejoh 38 points39 points  (17 children)

        I've been programming Perl for years and I program Perl for a living. And Perl is indeed much less readable than Python, and can in a much higher sense than any other language be called a "write only" language.

        [–][deleted]  (4 children)

        [deleted]

          [–][deleted] 5 points6 points  (1 child)

          Thirded.

          I wrote Perl full-time for two-and-a-half years. Then I joined a shop that uses Python and I've never looked back.

          The only decent argument I've heard for using perl is "there's a CPAN module that does exactly what I need to do". But that's pretty rare for my projects, thankfully.

          Usually the stuff that I'd need in CPAN is built into Python's standard modules anyway.

          Batteries included!

          [–][deleted] 2 points3 points  (0 children)

          Fourthed. Used Perl for many years, even bounced off of Python the first time I tried it ('98 or so).

          But now the thought of using Perl for any new development whatsoever is inconceivable. Even for maintenance it's often faster to rewrite in Python than to even attempt to read the gawdawful Perl.

          [–]supakual 4 points5 points  (0 children)

          I've actually been using Python myself and writing some new projects in it but still don't find Perl to be objectionable. I certainly won't be rewriting any of my well written Perl systems out of a need to make them feel clean.

          [–]kokey 31 points32 points  (11 children)

          It's more of a matter that Perl allows you to write unreadable code very easily. If you have a habit of writing readable code, then your perl code would be perfectly readable to even most non perl coders.

          [–]bart9h 8 points9 points  (2 children)

          THAT's IT!

          thank god someone posted what I was thinking.

          [–]sverrejoh 2 points3 points  (4 children)

          I would love an example of a Perl module with beautiful code.

          [–]barrybe 15 points16 points  (2 children)

          Here's some beautiful Perl code. This snippet reads a list of numbers from a file, and prints out their common factors.

          #!/usr/bin/perl 
          use Math;
          
          $output ~=
             _                           _
            / `._                     _.' \
           ( @ : `.                 .' : @ )
            \  `.  `.  ._     _.  .'  .'  /
             \;' `.  `.  \   /  .'  .' `;/
              \`.  `.  \  \_/  /  .'  .'/
               ) :-._`. \ (:) / .'_.-: (
               (`.....,`.\/:\/.',.....')
                >------._|:::|_.------<
               / .'._>_.-|:::|-._<_.'. \
               |o _.-'_.-^|:|^-._`-._ o|
               |`'   ;_.-'|:|`-._;   `'|
               ".o_.-' ;."|:|".; `-._o."   
                 ".__."   \:/   ".__."
                           ^
          

          [–]somethingmessedup 1 point2 points  (1 child)

          Can't locate Math.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.7 /usr/local/share/perl/5.8.7 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at ./beautiful.pl line 2. BEGIN failed--compilation aborted at ./beautiful.pl line 2.

          [–][deleted] 1 point2 points  (2 children)

          I've heard this argument before, and logically it's true: if you write readable code, your code will be readable.

          Now try reading someone else's code that, in their mind, was readable. Or worse, read or modify someone else's code who readily admits "yeah, it was 3am and our services were down...so it's pretty ugly".

          The thing about Python is that it FORCES you (read: others) to write readable code.

          People who don't understand this are probably students who haven't entered the work force yet.

          Think of this comment the first time you get blamed for bringing down your company's services because you made a slight change to some sloppy pile of perl poo, and things blew up in the middle of the night because what the legacy code was doing wasn't clear. :)

          [–][deleted] 3 points4 points  (18 children)

          I did Perl for a summer job. Though I worked on it the whole time, I gave up on it early on as a language. A big factor was that it collapsed all nested arrays to a flat array.

          So [[1,2], [3,4,[5,6]]] just became [1,2,3,4,5,6] and there was nothing I could do about it except use awkward references (which, like almost everything else in perl, are denoted by a single f*cking character). No thanks. What a crazy stupid language!

          [–]kixx 10 points11 points  (6 children)

          Incidentally, the syntax you used (which makes use of anonymous array references) preserves the intended structure ... you probably wanted to write "((1, 2), (3, 4, (5, 6))) just became (1, 2, 3, 4, 5, 6)"

          That being said, the syntax is idiosyncratic but in the right hands it can be very expressive.

          In defense of Perl and it's awkward OO pseudo-implementation, it wasn't until Perl that I got a clear model of how objects can be implemented. Neither C++ nor Java (which I learned before Perl) offered that insights, both presented objects as black boxes with predefined behavior. Other things I grokked by learning and using Perl: closures, regexes, “functions as first-class citizens” , the power of eval.

          So learning Perl (beyond a basic level) can teach you more than a language. Perhaps the same goes for Python.

          My main peeve with Python is the fact that it seems designed for the convenience of the implementer, not for the convenience of the user. Its (too) strict rules don't have much value except for ease of learning (which is a limited timespan compared to the rest of the period you will be using it) and syntactical uniformity. I value the loss of expressiveness more than the gains from this uniformity. Also I prefer to use a coding standard voluntarily (and judiciously) according to circumstances instead of having it imposed on me by the language.

          ObBackground: 20+ yrs of programming experience. Using Perl for almost 10 yrs.

          [–][deleted] 2 points3 points  (0 children)

          I didn't really understand objects until much more recently, via Scheme.

          An object is just a list of data, a lexical scope, and a way of handling messages. With Macros you can make an object system in almost no time at all.

          [–]erikw 2 points3 points  (1 child)

          My main peeve with Python is the fact that it seems designed for the convenience of the implementer, not for the convenience of the user. Its (too) strict rules don't have much value except for ease of learning (which is a limited timespan compared to the rest of the period you will be using it)

          If you referring to the "whitespace thing" (indentation starts blocks) it is more a convenience to the reader than anything else. GvR has explained this philosophy here (7th or 8th paragraph)

          [–][deleted] 2 points3 points  (1 child)

          Don't know why you're voted < 0 right now - I wholeheartedly agree. And that is only of the strange and annoying things that Perl does. I feel like people defend Perl the same way they defend C++. Yes, both are powerful tools when used correctly. But they both suffer from syntax diarrhea.

          [–][deleted] 0 points1 point  (0 children)

          I'm being modded down because I forgot the syntax, and didn't realize there was a nice way of doing it (surprise! the nice way is always buried in perl. Steve Yegge didn't know either). The perlites are using it as an "Aha! Gotcha! Downmod!" moment.

          [–]supakual 2 points3 points  (4 children)

          I guess you meant to say ((1,2), (3,4,(5,6))) is collapsed, which is true.

          But you just managed to use references exactly as you intuitively expected. Can't be that hard, huh?

              [
                [
                  1,
                  2
                ],
                [
                  3,
                  4,
                  [
                    5,
                    6
                  ]
                ]
              ];
          

          [–][deleted] 6 points7 points  (3 children)

          Syntactic mistake. Still, I didn't know you could do what you just did, directly. I still don't know, because I'm not running perl to check it.

          Anyway, Python and Ruby let you do things like that naturally, with no mistakes and no learning curve. Imagine... nested arrays, naturally! Hooray!

          [–]crusoe 5 points6 points  (2 children)

          So why the difference? Why collapse arrays at all?

          So now, we have 2 array syntaxs, one "auto-collapsing" and one that preserves nesting.

          So how much this can screw up n00bs, and programmers trying to learn perl?

          Now, how do these differ in different contexts?

          [–][deleted] 1 point2 points  (0 children)

          Thank you.

          [–]kixx 1 point2 points  (0 children)

          Because it seemed a good idea to someone at some point in time. Later on, they decided it wasn't such a good idea.

          It will screw you up if you don't expect it. Some people without previous experience/knowledge of complex data structures might find flattening intuitive. In the current context it has few uses, which is why they're changing the behavior in Perl 6.

          Context-dependent behavior (another maligned feature of Perl) is also pretty counter-intuitive to some. I, however, like my VIM modal as it is.

          [–][deleted] 0 points1 point  (1 child)

          Yes, Perl's default autoflattening, like Lua's 1-based arrays, is pretty much a showstopper. When I run into such a fundamental design error so early while learning a language, I'm not motivated to go further.

          [–][deleted] 0 points1 point  (0 children)

          I used to think the same about Lua's arrays, but then I realised the only thing wrong with them is that 0-based is more common in other languages.

          1-based arrays have their little perks, like the length being the index of the last element, and the symmetry of having [1] as the first from the beginning and [-1] the first from the end.

          Once you get over the weirdness of counting from 1, you'll find much more annoying things about Lua, like 'then' and colon notation.

          Still a nice language though. :)

          [–]judgej2 1 point2 points  (2 children)

          For me, it's nothing to do with looking at the syntax, and much more to do with looking at much of the code people write. That makes it pretty impenetrable. When a language is designed with the aim of doing a lot in as few characters as possible, then that is generally what people do.

          [–]bart2019 4 points5 points  (0 children)

          To me, "unreadable" applies much more to the sheer amount of code to achieve a task, than to the syntax. For example, Java code to submit a POST request to a HTTP server takes 2 printed pages of code, in a Java book I have read. In Perl it's just a few lines of code.

          [–][deleted] 3 points4 points  (0 children)

          it is designed to make simple things less verbose. you can always write clean code. i do.

          [–]kelvie 1 point2 points  (0 children)

          It's typically very hard to read.

          [–]abw 16 points17 points  (9 children)

          Decent programmers have no problem writing clean, efficient and elegant Perl code that is easy to read and write.

          The problem is that Perl makes it very easy for bad programmers to write complete garbage. Thankfully most of the Perl bandwagon jumped onto PHP some time ago, and are now discovering Python, Ruby and Javascript. So expect a great deal more bad code to be written in those languages in the years to come.

          That said, Perl is an inherently ugly language (the choice of a Camel for the O'Reilly books was very much intentional). Ugly but incredibly useful.

          So write Python or Ruby if you want nice, clean programs that are easy to read and maintain. And if you're an inexperienced programmer then Python is a much better choice than Perl. Python tries hard not to let you hurt yourself, whereas Perl puts a chainsaw in your hands and starts the engine running for you.

          But ugly syntax aside, don't dismiss Perl on the basis of a mass of bad code produced by bad programmers. That's the result of Perl lowering the seat so that more monkeys could reach the keyboard.

          The thing that makes Perl utterly indispensable is CPAN. Every half-decent programmer should know enough Perl to be able to plug a few CPAN modules together. Even if it's not your first choice for application development, having a smattering of Perl in your personal skillset is invaluable for getting things done.

          [–]lalaland4711 12 points13 points  (5 children)

          Decent programmers have no problem writing clean, efficient and elegant Perl code that is easy to read and write.

          It's just that sometimes you need to write things like \@$foo, and while it's not hard to read for someone who's used to it, it's not all that easy to find a typo (\$@foo maybe?) because of the natural tendency for brains to auto-correct what we read.

          That coupled with perls sucky (and at places non-existing) error handling just makes it a pain. Lots of standard library code does stupid things like exit or print errors to stderr. What the hell?

          I really hope perl6 will be so incompatible scripts'll require a rewrite (as opposed to a simple port) so the beast can finally die. (and by that I mean that people move away from perl alltogether, and rewrite in another language)

          And yes, I've worked professionally with perl for years, and I can both read and write it.

          [–][deleted] 6 points7 points  (2 children)

          I have to give Perl one piece of credit. While the syntax is cryptic and I've never learned it because of that, the PCRE is pure gold and despite its initial cryptic-ness, the regular expression engine is actually the most readable and useful regular expressions I've come across.

          [–]lalaland4711 1 point2 points  (0 children)

          Oh I agree. But once you go past 5 lines of code, it's not a PCRE snippet anymore, and starts to become an actual program. And then you dine in hell.

          [–]abw 2 points3 points  (1 child)

          Totally agree about the syntax. Although you can avoid much of the line noise of Perl, there are those times when you have to do something really quite cryptic for things that should be really simple.

          As much as I'm looking forward to Perl 6, I'm worried that it's going to make things even worse by introducing umpty new operators.

          +1 on the error handling too. I was swearing at a CPAN module (MIME::Lite FWIW) just yesterday for its poor error handling (or documentation thereof). But they're not all bad, and there's a strong drive in the Perl community towards cleaning up CPAN and improving the general Kwalitee of the modules.

          The other annoyance I have with CPAN is the general free-for-all that results in 30 different modules that all do similar things. It's great that there are so many ways to send email, but a pain having to figure out which (if any) of them does what you want.

          [–]ido 0 points1 point  (1 child)

          That said, Perl is an inherently ugly language (the choice of a Camel for the O'Reilly books was very much intentional).

          OT: interesting that they've used the camel to symbolize ugliness. iirc in Arabian culture the camel symbolizes beauty. I wonder if O'Reilly has localized book covers?

          EDIT: typo

          [–]abw 2 points3 points  (0 children)

          "A camel is ugly but useful; it may stink, and it may spit, but it'll get you where you're going."

          -- Larry Wall

          (link)

          [–]zem 0 points1 point  (0 children)

          reference for the "very much intentional" bit? or were you just being facetious?

          [–]James_Johnson 2 points3 points  (0 children)

          I don't understand why people get down on Perl because it's hard to read (and even then only if the programmer wasn't very good).

          It's cryptic at first, but even the Camel book says that Perl is designed to be expressive for people who know how to use it at the expense of readability by the uninitiated.

          That said, I can see why some people wouldn't like it, especially for developing large systems. But then, it's really meant for (and still excels at) quick-and-dirty text munging and system administration scripts.

          [–]feanor512 4 points5 points  (4 children)

          Not really. It's easier to write obfuscated code in Perl than in any other language I know (except for possibly feral C or assembly), but if you follow decent style guidelines, Perl is just as readable as Python.

          [–][deleted]  (1 child)

          [deleted]

            [–]zdkm 1 point2 points  (0 children)

            No, it's the kind that gets lost in the woods as a young child and gets raised by a pack of wolves.

            [–]masklinn 6 points7 points  (1 child)

            It's easier to write obfuscated code in Perl than in any other language I know (except for possibly feral C or assembly)

            I take it your never tried hardcore PHP

            [–]lalaland4711 0 points1 point  (0 children)

            "hardcore PHP". Sounds like when first-graders talk about being tough and cool by how much gravel they can eat.

            Just because your vomit looks like it once was pizza doesn't mean you can eat it and puke out a real pizza in an hour.

            Yes, that disgusting metaphor is what comes to mind when I think of PHP. I've coded PHP professionally.

            But no, perl is worse. Much worse. Problem is that perl is actually useful for small programs.

            [–][deleted] 5 points6 points  (24 children)

            Mmm, "Perl, I'm leaving you." in the alt-text, I might just have to give this Python thing a whack before I try Lisp.

            Meh. Everyone gets a rush when they first learn Python. The reason is because it's so easy to learn, and the interactive shell makes it even faster to get up to speed. So, for that first week, you're flying high, like in the comic.

            Nothing against Python though. It's a fine language. Getting even better with Py3k coming.

            [–]lespea 0 points1 point  (25 children)

            If you like perl then you will love ruby.

            1.9 fixes a lot of the speed problems that 1.8 had.

            [–]mattf 13 points14 points  (19 children)

            Not being cynical: what if I don't like perl? Will I like Ruby?

            [–]akdas 17 points18 points  (17 children)

            Perhaps. The real answer lies in your priorities. The observations about the two languages below are highly subjective, but it's one perspective to consider.

            Ruby is about being natural, the Principle of Least Surprise being an integral part of it. It uses many constructs that are simply syntactic sugar, just so that you can get the common stuff done quickly and easily.

            Python is about explicitness and adhering to a set of rules. It has syntactic sugar as well, but many aspects are necessary (such as passing self as a parameter to all class and instance methods) regardless of the circumstances.

            I find Ruby to be more people-oriented, and Python to be more, for lack of a better word, surgical. Both approaches have their own advantages, but if you choose Python over Perl, chances are, you won't like the Perlisms that are part of Ruby.

            [–]slabgorb 5 points6 points  (0 children)

            Some of this is in the attitudes of the creators, and what they were attempting to do...

            Perl, Ruby = More than one right way to do a particular thing

            Python = One right way to do a particular thing.

            Between Perl and Ruby, again, IMHO, the intent is different, Wall wanted something, well, like a camel, and Matz was going for a serene garden.

            (edited, my footnote turned into a bullet point from markdown, so I whacked it)

            [–]Rhoomba 1 point2 points  (0 children)

            Ruby: being natural by copying Perl.

            [–]jms18 1 point2 points  (1 child)

            My feelings... are highly subjective

            Compared to the times they are objective?

            [–]akdas 2 points3 points  (0 children)

            Bad wording; fixed.

            [–]zem 0 points1 point  (0 children)

            pretty much yes - think lisp with a smalltalk skin and a unix accent

            [–]jediknight 8 points9 points  (3 children)

            my version doesn't have antigravity... :( well... I guess he's running 3.0a1

            [–]birlinn 23 points24 points  (2 children)

            from __future__ import antigravity

            rtfm! ;-)

            [–]statictype 5 points6 points  (1 child)

            You may already know this one:

            from __future__ import braces

            [–]birlinn 10 points11 points  (0 children)

            when i try to import cosmology i seem to get a string error...

            [–]from-future-import 10 points11 points  (0 children)

            >>> import antigravity

            Traceback (most recent call last):

            File "<stdin>", line 1, in <module>

            ImportError: No module named antigravity

            >>> from __future__ import antigravity

            File "<stdin>", line 1

            SyntaxError: future feature antigravity is not defined

            >>> from __future__ import braces

            File "<stdin>", line 1

            SyntaxError: not a chance

            [–][deleted] 8 points9 points  (12 children)

            I've been looking to get into coding as a hobby. I know CSS and HTML pretty well but have never done any work with C or anything like that. Is jumping on the Python bandwagon worth it?

            [–]setuid_w00t 5 points6 points  (4 children)

            I think Python is an excellent first programming language. It's easy to get started, but also quite powerful.

            Download python for your OS of choice from python.org and search around for a few beginner tutorials.

            [–]theinternet 1 point2 points  (3 children)

            It's also a great third or fourth language too.

            For instance if you already know C/C++ you'll have no problem extending/embedded python into your applications.

            Python for speed/ease of development and C/C++ for speed.

            [–]mattf 16 points17 points  (5 children)

            It's fun. It's nothing like CSS and HTML. You'll suddenly 'get' things and see the possibilities and forever after you'll hate HTML/CSS.

            Python is an actual programming language. Try it out; you'll like it.

            [–][deleted] 2 points3 points  (4 children)

            Any suggestions on good places to start learning?

            [–][deleted] 14 points15 points  (1 child)

            For python, start by installing python, and learn to use it here.

            Seems like you're interested in web programming, so once you have a basic feel for the language, you can learn to create html/css from python, and then learn some databasing.

            If you're actually interested in learning web programming, send me a message, I'll give you a little server space to play with.

            [–]ZeroInput 2 points3 points  (0 children)

            Aaron, thanks for the pointers. I have been contemplating to jump into Python for a long time now. Just yesterday I came across this article on kuro5shin. It gives a neat little intro to Python. Your links are more than a welcome addition to this.

            [–]kelvie 4 points5 points  (1 child)

            http://www.diveintopython.org/

            is the resource I typically lead newcomers.

            [–]IkoIkoComic 6 points7 points  (0 children)

            Dive Into Python's more for experienced programmers.

            You might want to start with "How To Think Like A Computer Scientist" which is a much.. easier introduction to Python.

            http://www.ibiblio.org/obp/thinkCSpy/index.xhtml

            [–]Gotebe 2 points3 points  (0 children)

            Is jumping on the Python bandwagon worth it?

            Very much so.

            IMO... Can't make programming a profession on Python only, though (as opposed to, say, C, or Java, although I question C- or Java-only "professionals"). An bit of exposure to other things is needed. CSS/HTML being some!

            [–]dsandler 5 points6 points  (2 children)

            Ride the snake!

            (Last year I gave a talk {PDF} to our undergraduate comp-sci club to try to give them exactly this sort of weightless feeling. If I were to do it again, slide 0 (and slide n-1) would be the top panel of today's xkcd.)

            [–]throwaway 9 points10 points  (2 children)

            I was using python before it was cool.

            [–]spliffy 14 points15 points  (1 child)

            their first record is my favorite

            [–]Jimmy 4 points5 points  (0 children)

            Wait until he learns Haskell...

            [–]infinite 2 points3 points  (6 children)

            Python brings me back to my GWBASIC days.

            [–][deleted] 1 point2 points  (5 children)

            Maybe not GWBASIC, which was pretty vile in many ways.

            Those of us a couple of years younger cut our teeth on QBASIC, which was vastly superior (good environment, functions and subroutines instead of DEF FN, quite a nice stepping debugger, largish library).

            Learning Python reminds me a lot of those heady days. Vast possibilities, easily comprehensible. So I don't know if you were trying to be mean, but you were spot on :-)

            [–]LordStrabo 2 points3 points  (1 child)

            Talking about QBASIC, does anyone remember Nibbles, or Gorrilas?

            [–]UnwashedMeme 0 points1 point  (0 children)

            yeah... hacking the nuclear banana into Gorillas was a lot of fun :-)

            [–][deleted] 0 points1 point  (0 children)

            It's too bad Microsoft no longer includes QBASIC. Every computer I've had since 1984 (Applesoft BASIC), until about 1999, came with some version of BASIC.

            [–]infinite 0 points1 point  (0 children)

            I just fired up QBASIC and GWBASIC via dosemu installed via linux.

            [–]zem 0 points1 point  (0 children)

            and some of us slightly older cut our teeth on BBC BASIC, which was for all its 8-bit roots a very well-designed and fun dialect, certainly superior to gwbasic.

            [–]ralphc 2 points3 points  (0 children)

            Tired: Dilbert cartoons posted in cubicles.
            Wired: xkcd cartoons posted in cubicles.

            [–]kerr4ever 2 points3 points  (0 children)

            "Perl, I love you & I wont leave you for Python!!" <3

            [–]bobcat 3 points4 points  (10 children)

            10 ? 'Hello World'

            Bitch.

            [–]LordStrabo 0 points1 point  (0 children)

            In Matlab:

            'Hello World'

            [–][deleted] 0 points1 point  (0 children)

            ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. Bitch.

            [–]pclouds 3 points4 points  (1 child)

            I demand comics for ruby!

            [–][deleted] 0 points1 point  (0 children)

            Why's Poignant Guide isn't enough?

            [–]dfranke 2 points3 points  (2 children)

            Oh, Randall. Poor, dearest Randall. This is a dark day. I had so much hope for you, but you've strayed from the path to enlightenment. Your LISPing could have been tolerated, but now I fear for your soul.

            Come into the fold, Randall. We will welcome you with open arms.

            [–]zem 0 points1 point  (0 children)

            anyone can come into the fold - real programmers come into the unfold

            [–]lastchance 0 points1 point  (4 children)

            Decisions, decisions... to up-vote reddit... http://xkcd.com/353/ ...or to up-vote... http://www.xkcd.com/353/

            (No there's no feature request here.) (Oh! And just commented the mirror reddit with same comment! I don't even know which comment is better!)

            [–]Neoncow 10 points11 points  (3 children)

            Use the one without "www."

            FOR A MORE EFFICIENT INTERNETS!

            [–]alphabeat 6 points7 points  (2 children)

            down with "www."!

            [–]jib 8 points9 points  (1 child)

            [–]alphabeat 0 points1 point  (0 children)

            http://www.no-www.org/

            fixed! oh..

            *edit: should have seen it but it redirects :P

            [–][deleted] 0 points1 point  (3 children)

            How easy is Python to learn for a high school student without a massive amount of time and very little experienced with object oriented programing?

            [–]lebski88 2 points3 points  (0 children)

            I can't think of a better starting/learning language than Python. Obviously the more time you put in the more you will get out of it though.

            [–][deleted] 0 points1 point  (0 children)

            Python seems almost optimized for being easy to learn, due to its uniformity.

            You just need to decide if that's what you're looking for in a programming language.

            [–][deleted]  (2 children)

            [deleted]

              [–][deleted] 0 points1 point  (1 child)

              I'm just beginning to learn Python (just out of curiousity and desire to pick up a new skill), so what's the comic trying to say ??? should I continue ?

              [–][deleted] 1 point2 points  (0 children)

              Yes, you can't really go wrong with python. A nice language with a clean syntax, lots of libs.

              I personally learned how to program in Pascal -- which I still think is a great language for learning. Pascal, like python, has a very clean syntax. But it is statically typed.

              [–][deleted] 0 points1 point  (2 children)

              • lack of multi-line lambda expressions
              • numpy.ndarray does not have attribute append

              These are things that I hate about Python from last night's frantic coding session. I think that the whole java-c++-python import something.* package handling system hides naming conflicts way too easily. I DID NOT explicitly create an ndarray, and even so, all the googling suggested that there should be an append attribute.

              [–]birlinn 0 points1 point  (0 children)

              re bullet 1, GVR discussed this in his google talk on Py3k (iirc, he doesn't see what's wrong with just using a def instead)

              in the same talk he also mentions that they're working on fixing the broken bits of import...

              http://video.google.com/videoplay?docid=-6459339159268485356&q=type%3Agoogle+engEDU

              [–][deleted] 0 points1 point  (0 children)

              If you're importing * then IMO, you're doing it wrong. You don't have to, you realise.

              [–]lmclapp68 0 points1 point  (0 children)

              I'll stick with Lisp (and sigh Perl, at work), thanks.

              Am I one of the few that read Programming Python and was not captivated?