views:

684

answers:

4

I've done some Python but have just now starting to use Ruby
I could use a good explanation of the difference between "self" in these two languages.

Obvious on first glance:
Self is not a keyword in Python, but there is a "self-like" value no matter what you call it.
Python methods receive self as an explicit argument, whereas Ruby does not.
Ruby sometimes has methods explicitly defined as part of self using dot notation.

Initial Googling reveals
http://rubylearning.com/satishtalim/ruby_self.html
http://www.ibiblio.org/g2swap/byteofpython/read/self.html

+3  A: 

Well, I don't know much about Ruby. But the obvious point about Python's "self" is that it's not a "keyword" ...it's just the name of an argument that's sent to your method.

You can use any name you like for this argument. "Self" is just a convention.

For example :

class X :
    def __init__(a,val) :
        a.x = val
    def p(b) :
        print b.x

x = X(6)
x.p()

Prints the number 6 on the terminal. In the constructor the self object is actually called a. But in the p() method, it's called b.

Update : In October 2008, Guido pointed out that having an explicit self was also necessary to allow Python decorators to be general enough to work on pure functions, methods or classmethods : http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html

interstar
+2  A: 

self is used only as a convention, you can use spam, bacon or sausage instead of self and get the same result. It's just the first argument passed to bound methods. But stick to using self as it will confuse others and some editors.

Vasil
+5  A: 

Python is designed to support more than just object-oriented programming. Preserving the same interface between methods and functions lets the two styles interoperate more cleanly.

Ruby was built from the ground up to be object-oriented. Even the literals are objects (evaluate 1.class and you get Fixnum). The language was built such that self is a reserved keyword that returns the current instance wherever you are.

If you're inside an instance method of one of your class, self is a reference to said instance.

If you're in the definition of the class itself (not in a method), self is the class itself:

class C
  puts "I am a #{self}"
  def instance_method
    puts 'instance_method'
  end
  def self.class_method
    puts 'class_method'
  end
end

At class definition time, 'I am a C' will be printed.

The straight 'def' defines an instance method, whereas the 'def self.xxx' defines a class method.

c=C.new

c.instance_method
#=> instance_method
C.class_method
#=> class_method
webmat
What should be done about an answer like this that is factually wrong? The very first version of Python published had a full class system.
John Millikin
Decided to just edit out/correct the false part, but it'd be nice if answerers would refrain from posting things that are easily refuted by a glance at wikipedia.
John Millikin
Ouch! Good catch. I'm actually not a Python developer, I should have put a disclaimer in there. I went from memory from an interview about 2 years ago with Guido van Rossum (on FLOSS Weekly)
webmat
Its also worth noting that literals in Python are full objects just like in Ruby. Evaluating (1).__class__ gets you <type 'int'>. There is a difference in the tokenizer (unrelated to the object system) that treats "1." as a float, but using brackets, or "1 .__class__" will work as expected.
Brian
+1  A: 

Despite webmat's claim, Guido wrote that explicit self is "not an implementation hack -- it is a semantic device".

The reason for explicit self in method definition signatures is semantic consistency. If you write

class C: def foo(self, x, y): ...

This really is the same as writing

class C: pass

def foo(self, x, y): ... C.foo = foo

This was an intentional design decision, not a result of introducing OO behaviour at a latter date.

Everything in Python -is- an object, including literals.

See also Why must 'self' be used explicitly in method definitions and calls?

Matthew Trevor