Tuesday 2 March 2010

The Joy of Self

I have a distant and fuzzy memory, from back in the day... when I was but a wee slip of a lad, sallying forth to do battle with million-line C++ monstrosities (and just barely escaping with my sanity intact purple monkey dishwasher), I came upon a Path class. It was probably called CPath: classes were new and shiny, so far as any of us knew at the time, and absolutely deserved a prefix to underline their special status. Look, ma, I'm programming Object Orientedly!

And, yeah, it was horrible. Big, clunky, confusing... I'd like to say that the blistering speed made up for it all, but that would be entirely untrue. It was nasty.

As a result of this, I had never since felt the slightest urge to create a Path class of my own... until today. I was trying to get an overgrown build script under control, and - despite my scars - it suddenly seemed like a good idea.

So I had a go.

And... well, this sort of thing is why I love Python, and is also the clearest illustration I've yet seen of why explicit self is a Good Thing. The following code is, as usual, hacked up from memory and may therefore contain hilariously deadly bugs; caveat lector.

import os, shutil

class Path(str):

def __new__(cls, path):
abs_ = os.path.abspath(path)
norm = os.path.normpath(abs_)
return str.__new__(cls, norm)

exists = property(os.path.exists)
isfile = property(os.path.isfile)
isdir = property(os.path.isdir)
# etc...

move = shutil.move
listdir = os.listdir
# etc...

def __getattr__(self, name):
return self.join(name)

def join(*args):
return Path(os.path.join(*args))

def delete(self):
if self.isdir:
shutil.rmtree(self)
else:
os.remove(self)
# etc...

Now, IMO, this was a massive win: I made the client code a lot less verbose, and hence clearer, and I did most of the work by trivially subclassing a builtin type and dropping in a bunch of standard library functions as methods. The real one has many more bells and whistles (in fact, I think I got a bit carried away) but hopefully you get the idea.

The crucial point is that I couldn't have done it so neatly without explicit self; also, amusingly, most of the explicit selfs in this class are in fact implicit. So there.

6 comments:

Dirkjan Ochtman said...

You mean, like this?

http://pypi.python.org/pypi/path.py

I think it was even discussed for inclusion in the stdlib at one point.

william said...

Thank you for pointing me at that; I notice a couple of cute tricks I might well borrow.

However, I think the one I wrote has a few features missing from that one - which itself has lots of features I don't really need - so I feel somewhat vindicated in building my own ;-).

Lucian said...

Heh. I never really understood why some people complained about it. It's a design decision and it doesn't impact productivity at all.

I wish a tamed path.py entered the stdlib at some point. Perhaps py.path (from the py library)?

Michael Foord said...

Guido didn't like the fact that path subclassed str, and also that it basically duplicated the functionality currently scattered across os, os.path, stat and shutil.

He did however leave the way open for an object-oriented path that didn't subclass str to enter the standard library at some point in the future.

Will McGugan said...

You may want to check out my filesystem module. IMHO it often negates the need for a path class...

http://code.google.com/p/pyfilesystem/

Ian Bicking said...

Not "practical" per se, but another take on this idea: with DictMixin