views:

121

answers:

6

The handle to yourself is called different things in OOP languages. The few I've come across so far:

  • this (e.g. Java, C#)
  • Me (e.g. VB, vba)
  • self (e.g. Python)

Anyone know any others?

+3  A: 

In Python, it is just a convention that the zeroth argument is called self. What matters is the position. Anything will do, so you could use i or anything else:

class Foo:
  def bar ( i ):
    print i
Pete Kirkham
interesting! I didn't realize that
curious
Pardon the pedantry, but "zeroth" really grinds my gears. The first ordinal number in English is, well, "first". If we allow "zeroth", then the item you refer to as "first" is really the second, and that just gets confusing. And it means that when Madonna was talking about being "touched for the very first time", she wasn't really referring to the time she lost her virginity. The song would've had to be "Almost Like a Virgin", and that just doesn't have the right rhythm.
P Daddy
@P Daddy It comes from a method being called by `zeroth.func ( first, second, third, etc )`, and a free function being called `func ( first, second, third, etc )` - the first argument is conventionally the first of those inside inside the argument list when called, and the zeroth one is the one before the first argument. By not having a keyword for it, Python confuses the convention somewhat by having the zeroth argument as the first argument in the definition, but not at the call site.
Pete Kirkham
+1  A: 

The search for self...

Most often it's nothing at all. For instance, usually "x" will refer to this.x if no local x variable exists.

Bill K
+1  A: 

In Perl, a reference to itself is never implicit.

sub work {
    my($self) = @_;
    sleep();         # Don't do this
    $self->sleep();  # Correct usage
}

source: "Writing serious Perl - The absolute minimum you need to know"

S.Jones
Though by convention most perl programmers name that reference `$self`. It's just the first argument to the method.
slebetman
@slebetman Yes, you're absolutely right. I was way too brief in my answer. I've added an example illustrating the convention.
S.Jones
A: 

Smalltalk also uses self.

Tom Anderson
A: 

F# is similar to Python and Perl, in that you just supply your own name. Here's @Pete Kirkham's Python example in F#:

type Foo =
  member i.bar = printfn "%O" i

Use it like so:

let x = new Foo()
x.bar
Jörg W Mittag
+1  A: 

In multiple-dispatch OO languages like Common Lisp (CLOS), Dylan or Slate, there is no single receiver object, and therefore no notion of self.

Jörg W Mittag