This is

as days pass by, by Stuart Langridge

. Here I write about many things. In the past I wrote about other things but the past is past. I write code for people to play with, I write about my life on Twitter, and I write here.

On Mar 04, 2005 I wrote Why I like Python, on the subject of Rants.

Schwuk makes reference to a post of Rory’s on why Python is no good. As Schwuk says, I might get annoyed by this, and he’s pretty much correct here.
You see, I think that a reasonable proportion of Rory’s complaints are a bit lacking. Take his complaint that you have to explicitly pass “self” to methods:

Sure, having to add self to a parameter list once isn’t any big deal, but three-hundred methods later, when the ink is worn off the S, E, L, and F keys, you might be thinking differently about it.

Take a glance at your close-curly-bracket key: }. Has the ink worn off that? If it has, then I’ll start to listen to your complaint more. If it hasn’t then, well, obviously extra unneeded characters, like C’s curly brackets, aren’t that big a deal, eh?
I would have respected a choice to have ditched all 1.x hacks, but instead they were carried over to 2.x, so now we had the choice of doing things procedurally or Object Orientedally (yeah – I made that word up – bite me). What a bloody mess! Let’s see… Do I want to work with functions on my strings, or do I want to work with members of the string object? I hate that kind of crap.
I utterly don’t agree here. OO gives you the power to write decent proper applications; I entirely agree, and I pretty much find that anyone writing anything even vaguely substantial in Python uses OO to do it. But constricting the user to write in OO style is a massive pain in the arse when you just want to throw out a quick script. I find myself doing things like
grep "some regexp" /some/file | \
python -c "import sys;print '\n'.join([x.split()[3] for x in sys.stdin.readlines if x.split()[6] == 'word'])"
If I had to wrap that in a load of OO bullshit it’d be loads more complex for no reason. Similarly, a lot of the Python I write is short scripts to, say, grab an RSS feed and rip some data out of it (the thing that reposts my SitePoint articles here is like that). Again, this is a quick procedural script: start at the top and work through the lines. They quite often don’t even use functions. If I had to swaddle that in OO bollocks it’d be more complex and slower to write and I would derive no benefit from it.

Notate bene primus: Perl people are saying “what’s all that sys.stdin.readlines() shite? This is what perl -n is for!” They’re right; Perl is even better at this sort of thing than Python is. I find that Python hits the sweet spot between the two; others differ.

Notate bene secundus: yes, I suppose I could do similar stuff with a sed/awk pipeline without going into Python. Fuck off.

Notate bene tertius: “orientedly“.

He’s right about all the underscores, though. Constructors being called __init__ is a pain in the arse.

In short, I disagree. However, each to their own and all that…

Update: further discussion on Schwuk’s original post.

[image]Senji

OO is bollocks period :-).

(But then I don’t write python anyway)

[image]mrben

I think that OO can be excellent, but, like so many things, can be bollocks if it is done purely for its own sake.

I use Python almost exclusively, even for things that would often be shell-scripted. It rocks ;)

[image]Rory

I think you misunderstood my argument (or I simply didn’t communicate it well enough).

What I don’t like is the either/or approach. Python can maintain all its simplicity and strip itself of the Old World without having to go down some convoluted path.

Just as C#/Java apps have the concept of Main() (or main()), Python could have something similar.

The difference here is that you guys are talking about scripting, and the given example is very short. Mixing styles in such a short example isn’t a big deal, but would get pretty complicated pretty quickly for a full-blown app.

I guess it’s just that I think Python would be more useful if it didn’t have this duality to it.

[image]sil

Oh, I agree entirely that mixing styles in a larger app would be complex, but the solution there is: don’t do that. And indeed, people do not; I’ve never seen a Python app of even reasonable size which isn’t through-and-through OO.
My point here is: I don’t understand why it’s bad that it can be procedural. If you removed the procedural stuff then (a) it would not improve your ability to write larger apps (since they are all OO anyway) and (b) it would make it more difficult to write short scripty stuff. That, to my mind, is a net loss, and it is in no way balanced (again, in my mind) by a rather nebulous concern that it might be more confusing to allow both. Unless I’m still misunderstanding your point?

[image]Lorenzo Gatti

I don’t think there are two alternative Python styles, procedural and object oriented; rather there are two layered OO styles, just using objects or defining classes and methods before using objects, and Java always requires the latter for no good reason.

Simple programs like the split/join one-liner in the example need only classes from existing libraries and complex programs need their own abstractions; but Python is completely object oriented in all cases, even when fancy features like comprehensions and generators are used.

The influence of dumbed down languages like Java has convinced people that

class Example{
  public static void main(String[] args){
    System.out.println("Hello world");
  }
}

is more object oriented and better than

print "Hello world"

or

#include <stdio.h>
int main (int argc,char**argv){
  printf("Hello world");
}

but the three programs do the same thing with the same object (the string) and vary only in the amount of additional cruft.
In fact the Java version, with its two useless classes Example and System, is a worse style of OOP than the unencumbered C version.

[image]Andy Todd

If you make Python completely OO you run the risk of turning it into Java. One of the things I like about Python is it isn’t so strictly straight jacketed as Java (or C#). The canonical example of this is stand alone functions. In Python you just create them at the module level, in strict OO you end up with a class with static class methods [1]. I know which I’d rather type.

OO is good, but only when I need it, and I don’t always need it.

[1] http://dirtsimple.org/2004/12/python-is-not-java.html

This website belongs to Stuart Langridge. Contact details are available. Don't eat yellow snow. Valid HTML5, at least in theory, except for the bits that aren't because I'm that futuristic that I'm ahead of the spec, oh yes. HTML5 help from Bruce Lawson, among others. Fonts from the superb FontSquirrel. End.


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser