Log in

25 May 2006 @ 02:04 pm
Python: the drain snake of programming.  
Every once in a while, someone notices I don't like Python much and wants an itemized list of reasons. It's not very easy to come up with such a thing on demand. I don't usually sit around making lists of reasons I dislike a given programming language, believe it or not, so I don't usually have such reasons in front of me. The short, flippant answer I tend to give instead of an itemized list is usually "It makes my eyes bleed."

Here's a little bit of a reason, though, from a conversation in IMs today:
(13:59:59) ilcylic: holy crap python is way different from perl.
(14:00:29) @: What did you notice is "way different"?
(14:01:16) ilcylic: just all the stupid OO language.
(14:01:32) @: Scaffolding an' stuff?  Egregious overuse of "self"?
(14:01:46) ilcylic: yeah.
(14:01:49) ilcylic: Oh yeah.
(14:02:06) ilcylic:
template = self.template('admin/article/index.pt')(context=self.context, **dict(self.options))
        self.form = HTMLForm(template, ArticleFilterSchema)

    def getDefaults(self):
        defaults = {'active':self.request().field('active', 'true'),
                    'featured':self.request().field('featured', ''),
                    'categoryID':self.request().field('categoryID', ''),
                    'locationID':self.request().field('locationID', ''),
                    'year':self.request().field('year', str(DateTime.today().year)),

(14:02:30) @: OUCH.
(14:02:39) ilcylic: yeah.
(14:02:39) @: You're making my EYES BLEED!
(14:02:47) ilcylic: and the use of tabs instead of braces
(14:02:52) @: yeah
(14:02:56) ilcylic: for flow control.
(14:03:03) ilcylic: makes it hard for me to read what's going on.
(14:03:11) ilcylic: like reading a book without punctuation.
(14:03:16) @: There's a distinct lack of symmetry.

I particularly like the punctuation analogy. Tomorrow I may not like it as much, but for now, it just sounds right.

NOTE: I intentionally changed the indentation on the first line of that Python code to try to neaten it up a touch for LJ.
Mood: amusedamused
neidukesnorre on May 25th, 2006 08:30 pm (UTC)
Mistar, I forget, are you on anything but IRC?
Autolatristapotheon on May 25th, 2006 08:35 pm (UTC)
AIM, ICQ, MSN Messenger, Y!M, email . . .
neidukesnorre on May 25th, 2006 08:36 pm (UTC)
Well, hand'em over. I can make a metacontact out of at least the AIM, ICQ and MSN bits.
Autolatristapotheon on May 25th, 2006 09:04 pm (UTC)
I did.
Brian Martinezcluebyfour on May 25th, 2006 08:51 pm (UTC)
I know nothing about python, but is it correct to assume that self is like this in other languages? Wouldn't it be implicit and thus optional, or must it be explicit in python?
Autolatristapotheon on May 25th, 2006 09:01 pm (UTC)
My understanding is that there's a "simple" (relative term) set of rules for when it's optional and when it's not, and sometimes when optional you're supposed to use it anyway because in Python there may occasionally be more than one way to do it, but only one of them is considered "right".

self is indeed like this, or even me, in some other languages.
ex_req431 on May 25th, 2006 08:56 pm (UTC)
Egregious overuse of "self"?

Autolatristapotheon on May 25th, 2006 09:04 pm (UTC)
ex_req431 on May 25th, 2006 09:18 pm (UTC)
very much so, darling
Brian Martinez: something positive rippy the razorcluebyfour on May 25th, 2006 10:37 pm (UTC)
Obviously, in python you have to touch your self a lot.

Oh, I kill me.
scoth: toonscoth on May 26th, 2006 01:42 am (UTC)
The OO python code I've encountered isn't nearly as horrendous as that; this behemoth isn't representitive of what python code usually looks like. It's really easy to write ugly code in any language.

As far as 'self' goes, it's generally considered good practice to explicitly reference the scope of the variable that you're using so that there's no confusion if you're referencing a local variable only valid for the current subroutine or a property that belongs to the instance of the object that you're using.

I can't do much without knowing more of the context but I've beautified part of that code for you. Is this less bleedy?
def getDefaults(self):
    req = self.request().field
    defaults = {
      'active':     req('active', 'true'),
      'featured':   req('featured',   ''),
      'categoryID': req('categoryID', ''),
      'locationID': req('locationID', ''),
      'year':       req('year', str(DateTime.today().year)),
scoth: toonscoth on May 26th, 2006 01:48 am (UTC)
like reading a book without punctuation.

I take it you perlistas don't read much poetry ;-)
Autolatristapotheon on May 26th, 2006 04:53 am (UTC)
I read plenty. Good poetry tends to be very easy to follow, even without punctuation — more like Ruby than Python.
Autolatristapotheon on May 26th, 2006 06:05 am (UTC)
. . . and I take it you haven't heard of Perl Poetry.
Autolatristapotheon on May 26th, 2006 05:07 am (UTC)
Not really. It still hurts.

Actually, while your changes don't make a huge difference in readability for this brief snippet of code, I can easily see how the principles you've applied to formatting this stuff, applied over a larger collection of code, could make a significant difference. It is, however, still a little hurty.

The end delimiter for the defaults declaration is a plus, which seems good at first glance for me, but in context it makes the lack of other end delimiters even worse. The lack of symmetry and clear indications of "togetherness" can be sort of a problem, especially when some indentation might be screwed up and it might be problematic trying to track down the problem when there aren't delimiters to give hints. At the very least, delimiter matching in text editors can help when indentation isn't the only way to separate blocks of code.

Even this much use of self kinda drives me batty, though. Go fig'.

Er, yes, less bleedy. Not unbleedy, though. That's the short version.
scoth: grrscoth on May 26th, 2006 06:29 pm (UTC)
There's just no pleasing you when it comes to python.

Are you done with the Perl Objects, References, and Modules book I loaned you?
Autolatristapotheon on May 26th, 2006 07:18 pm (UTC)
You could reformat it as Ruby or Perl. That would probably be pretty pleasing.

I'm not done, per se — still reading it. I've been doing a lot of rereading as I go along, to cement stuff in my brain, and I've been doing a lot of getting-distracted as well so that I'm not reading it as quickly as I could. On the other hand, I could easily return it without too much trauma, since I'm planning to buy the new Alpaca at my earliest convenience anyway. Just say the word, and I'll arrange to get it back to you.