Ugly Python Code

Screenshot of ugly Python code in a terminalContinuing my adventure in learning Python, I’ve frequently done searches for Python equivalents to various bits of PHP code and come across a lot of Python programmers bashing PHP. One of the most common Python arguments is that PHP, as a language, is ugly. Personally, I disagree. I think that a dollar sign in front of a variable makes it easy to identify that as a variable; no need to memorize 30 keywords. Consider how you would tell a non-programmer to identify a variable in Python (or how you would make a regex to get all the variables from a particular script). As far as braces go, they have never bothered me and I find it nice that Vim will show me the matching brace automatically. The idea of using whitespace for grouping makes sense to me and seems clean, so, really, the syntax of either language is fine for me.

The thing that actually bothers me is that so many people claim Python is inherently beautiful and PHP is inherently ugly. Unless braces and semicolons are the definition of ugly, I have to disagree.

Screenshot of PHP code in a terminalI think you can see from the sample screenshots that ugly code and beautiful code are largely caused by the coder rather than the language. These screenshots should give you an idea of what I am talking about. Both scripts are valid.

The PHP screenshot shows three different ways of doing if statements, none of which are the most common ones. They’re merely there to show that choices are available. The Python screenshot has a variety of “non-Pythonic” coding samples. The difference is that Python has a dictator who says, “I like things this way” and most Python programmers follow that preference. Of course I am sure that some Python fanatics would point out that PHP is inconsistent regardless of how nice you make the syntax, which is largely true… but so is Python. Consider the {}.has_key and {}.fromkeys methods or that you do len('string') but 'string'.istitle (”for historical reasons”).

I think the reality is that open source means multiple contributors with varying preferences. Consistency is important, but so is having flexibility. Calling PHP ugly with your reasoning being that you saw some newbie’s code and it was inconsistent is about as stupid as saying that paint is not a good medium because you saw a three-year-old’s fingerpainting. Sorry for another “Python isn’t perfect” post, but the elitist attitudes of some of the Python programmers I have read from were getting to me. A hammer is not better or worse than a screwdriver. Their original purposes were different, but they have some similarities such as their usefulness in attacking a Fortran programmer. ;)


6 Responses to “Ugly Python Code”

  1. 1 Mason

    I largely agree with you.

    I believe the inconsistencies in Python stem largely from the fact that they’re trying to transition towards object.method(), rather than the older function(object) syntax - the reason a string object duplicates many of the functions in the string module.

    During these “times of transition”, there’s always the awkward teenage years where things don’t quite look right… like PHP4 not supporting method chaining and having to explicitly pass by reference. Now I hear PHP is getting namespace support. Can’t wait to see where that leads us.

    I’m rather new to Python myself. Though I’ve heard the Python Mantra is “One Way to Do It”, I keep finding these little nagging bits that contradict it (decorator syntax, for example, is a shortcut for a rather long-winded alternative). I keep thinking, “If there’s only one way to do it, why are there… three ways to do it?”

    But again, it all comes down to the evolution of the language.

    By far the most consistent language I’ve used is Ruby. They’re mantra is to follow the “Principle of Least Surprise”, meaning it should be easy to guess the right way to do it. Going from your code example from the 13th, counting words in a sentence is as easy as:

    “this is a sentence”.split.length

    And if you really want a “word_count” function, you’re free to reopen the class definition and make it yourself:

    class String
      def word_count
        self.split.length
      end
    end
    

    Then you just go:

    “this is a sentence”.word_count

    Which of course, returns 4.

    Happy hacking.

  2. 2 Gordaen

    Both languages have definitely come a long way, especially after reading the various PEPs from the past. Going from PHP 4.2 to 5.0 was a pretty huge change (PHP 4.2 was the version that first made register_globals off by default) and it was a little over two years between those releases. I hope that PHP6 will show as much improvement.

    I have heard the same Python mantra and I think it’s often a case of “lots of ways to do it, but one way Guido prefers it.” The style guide says things like “Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.” Maybe those guides will actually be used in the majority of cases in the future and, if so, that would significantly help.

    I would have liked to have looked at Ruby a lot more. I liked what I saw, but with work moving to Python and my personal stuff in PHP, I don’t have enough time (or incentive, other than curiosity) to learn it. Your example suggests that it is very clear and it actually knows how to use “self” without telling it, haha.

  3. 3 Ink

    I don’t really think your comparison is all that fair. Your code highlighting is what makes the Python code very hard to read. The variables are the darkest words and your comments the brightest. Your strings a extremely overbearing as well.

    How you like to see the code is up to you, but that’s just a bad example. PHP would look just as bad if the variables were gray and the print functions were yellow (just like all the other keywords).

    I will give you props for being a badass and using VIM! That’s hardcore, dude.

    @Mason

    I love Ruby, myself. I think it’s a beautiful language for the most part - and definitely a powerful one. It’s great that it can be prototyped so easily and it’s a very simple language to learn.

    The thing that makes Python so great, is it’s ever growing community and contributions. Look at Google - they use python for almost everything. It scales better than most dynamic languages and because it is tab delimited most of the code is going to look familiar to other Python developers

  4. 4 Ian Clifton

    It’s not that my variables specifically are darker, but that anything Vim doesn’t recognize is that plain font color. Since Python doesn’t use any symbol to designate a variable in the way that C and other languages do, Vim doesn’t know to color the variables any differently. The colors are all default Vim colors (w/ bg=dark set).

    My point wasn’t meant to be made with the colors but with the style of code. Both examples are valid code and show that conventions are largely what make code readable (and, apparently, a good color scheme). You can have good conventions in PHP and you can have bad conventions in Python (and vice versa). Both languages are good, but they’re good at different things.

    If you haven’t started using Vim, I recommend it. The learning curve is like a wall at first, but every day you can find a new trick to save you time. Plus, a version of Vi or Vim is included in most Linux distros. I’ve used it for about 2.5 years and I still constantly uncover new tricks.

  5. 5 ink

    I understand what you are saying about the code being valid, it just doesn’t help that Vim doesn’t highlight the variables correctly. That would mean that ruby would have the same problem.

    If you really sat down and compared the languages, I think you would find Python to be quite a bit more powerful than PHP, in almost every aspect. I use to use PHP exclusively, and I loved it. It’s super fast to deploy for the web, but it’s not ideal for a lot of other applications.

    For me, at least, there’s no comparison anymore. I’ve worked with both languages and I don’t even feel like PHP is even in the picture. PHP doesn’t even have basic thread support. This is obviously just my opinion, but I think Lua, Ruby, and Python are the most mature dynamic languages. I also feel like they offer more opportunities to the developer to think outside there “web” box.

    Oh, and I don’t think I could use Vim exclusively. I like TextMate.

  6. 6 Ian Clifton

    I do find Python more powerful than PHP, but, as you said, PHP is extremely fast (and easy) to deploy for the web. If a client asked for his/her site to include a dynamically updated date on the main page, that would be two seconds of PHP work and little to no server work in most cases. The same generally can’t be said of Python. Consider what you would do to accomplish that in Python…

    Of course, when working on a large project, something like Django can be very effective, but I personally prefer the “nuts and bolts” approach of frameworks like CodeIgniter. PHP has plenty of limitations, but they’re not things that I run into, so they’re effectively non-issues for me.

    TextMate? Bah! I don’t know what I’d use if Vim didn’t exist. That does make me think of creating a video demo of some little Vim tricks though… maybe if I have time…

Leave a Reply