In Python, when you define a class, every method of that class must have "something" as the first parameter representing the object itself. We usually refer to this as "self" but as sepp2k pointed out to me, it can be called anything. Here's an example of self:
class dumbClass(): def __init__(self,x,y): self.x = x self.y = y def get_x_plus_y(self): return self.x + self.y
In the interpreter, we'd do:
>>> x = dumbClass(1,2) >>> print(x.get_x_plus_y()) 3
99% of the time, people make this first parameter 'self', the other 1% of the time you're dealing with someone who is inept or inexperienced. This has been one of the bigger gripes among Python users for years, particularly those with a Java background who fondly remember using 'this'.
Disclaimer: my first language was java
Like so many others, I personally feel that 'self' should be made implicit within classes. From a purely practical perspective, this would be a DRY optimization by removing a variable that should always have the same name and MUST appear at the beginning of every class method. It also will get rid of some cryptic error messages, for example:
class dumbClass(): def __init__(self,x,y): self.x = x self.y = y def calculateWinning(): print("not really winning")
When we test that in the interpreter
>>> x = dumbClass(1,2) >>> x.calculateWinning() Traceback (most recent call last): File "<pyshell#38>", line 1, in <module> x.calculateWinning() TypeError: calculateWinning() takes no arguments (1 given) >>>
An inexperienced user might respond with a good ol' "WTF?!". The error claims that I passed in an argument yet I most certainly did not explicitly pass one in. However, the problem is that the value of self is implicitly passed in, and thus I have in fact passed in 1 argument to a function that takes no arguments.
Is it not strange, neigh hypocritical that the object is implicitly passed in yet one of the biggest justifications of self is that explicit is better than implicit? I'm not drinking that koolaid anymore!
Another classic example of a problem associated with this was elegantly displayed by a newer user trying to learn Python:
class Character(object): def __init__(self, name, max_health, health, strength, stamina, toHit): self.name = name self.max_health = max_health self.health = health self.strength = strength self.stamina = stamina self.toHit = toHit def recover_health(self, amount): if self.health + amount > self.max_health: self.health = self.max_health else: self.health += amount
Notice how the indentation is off, but when he rand the code, it looked like this:
test = Character('John', 10, 10, 10, 10, 10) recover_health(test, 5)
The code was technically right, but it made it appear that OO in Python really sucks. While this example did come from some code a user recently posted, I assure you that I see similar issues very commonly.
EDIT: Because explicit is better than implicit, I'll also explicitly state: "putting self at the beginning of every method's parameters is annoying and tedious"
However, there are those out there who feel that 'self' is necessary for the good of all mankind. There are those who use the explicit > implicit argument (crap).
Others claim that it won't allow you to dynamically insert methods into classes.
# Define an empty class: class C: pass # Define a global function: def meth(myself, arg): myself.val = arg return myself.val # Poke the method into the class: C.meth = meth
I don't buy this one either. As baavgai would say, "Interpreted languages are interpreted". I think that the interpreter could be improved enough to handle this. If it can't, let's be honest, would you really miss this functionality? I wouldn't. I've never used it, I've never even dreamed of a use for it.
This particular blogger also claims that it would destroy decorators. Ehhh, if decorators are a blind spot for Bruce Eckel (who has made one of the most compelling anti-self arguments to date), it's a blind spot for me too, because I rarely use them. Again, I think that Python's interpreter could be improved enough that it can do a little "tree climbing" to see what "self" should be referring to, but I don't know them well enough to intelligently speak on that topic.
What do you guys think? Is 6 characters "self, " enough for people to get their feathers ruffled over? Are we too picky? Is this more of a principles argument? Let's get some "advanced discussion" going
This post has been edited by atraub: 20 August 2012 - 01:04 PM