Python has a wonderful, albeit confusing at first, requirement of listing self as the first argument accepted by object methods.
class AwesomeClass(object):
def __init__(self
, name):
self.name = name
def say(self
,message):
return "Hi my name is %s, %s" % (self.name, self.message)
>>> awe = AwsomeClass("frodrick")
>>> awe.say("its sunny out today")
Hi my name is frodrick, its sunny out today
To me this is wonderful. Not because I enjoy seeing self all over the place. Its wonderful because when I think of how computer programs work, it just makes sense. I also think it's important to meet computers where they are, to leverage their full power.
Here is what I think the explicit self enables:
Classes are more or less just a way of organizing objects of a similar type. So that a class can contain references to variables and methods and pass those references along to all the objects created by it.
If the objects contain variables, functions and a reference to their classes, which also contain variables and methods. a constructor is more or less just a way of appending references to a blank object. The explicitly stated self variable is a clear way to represent all that is happening, variables are being manipulated on an object that has been passed into this function. Which is especially important when functions can be passed around between objects.
def minipulate(obj):
obj.varA = "A Great Value"
obj.varB = "another setting"
class Foo(object):
def __init__(self, name):
self.name = name
minipulate(self)
JavaScript has a similar feature, but because it was "implicit" to reference whichever object called it, it was not very useful. And it broke inside of closures, javascripts most powerful feature. Because of this they introduced a builtin function named "call", but this didnt solve the closure problem. Closures are a way to reference information by creating a context for it.