What is the difference between the versions of "self" Ruby and Python?

I made some Python, but now I just started using Ruby. I could use a good explanation of the difference between the "I" in these two languages.

Obvious at first glance:
Self is not a keyword in Python, but there is a "self-similar" meaning, no matter what you call it.
Python methods take themselves as an explicit argument, while Ruby does not.
Ruby sometimes has methods explicitly defined as part of self, using dot notation.

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

+6
python ruby language-features
source share
4 answers

Python is designed to support more than just object-oriented programming. Keeping the same interface between methods and functions allows the two styles to interact more cleanly.

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

If you are inside an instance method of one of your classes, self is a reference to the specified instance.

If you are in the definition of the class itself (not in the 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 

A class ā€œI Cā€ will be printed in the class definition field.

The direct 'def' defines the instance method, while the 'def self.xxx' defines the class method.

 c=C.new c.instance_method #=> instance_method C.class_method #=> class_method 
+7
source share

Well, I don't know much about Ruby. But the obvious point in Python's "I" is that it is not a "keyword" ... it's just the name of the argument 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) : ax = val def p(b) : print bx x = X(6) xp() 

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

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

+5
source share

self is used only as an agreement, you can use spam, bacon or sausage instead of yourself and get the same result. This is just the first argument passed to the associated methods. But stick with the use of self, as this confuses others and some editors.

+5
source share

Despite the expression webmat, Guido wrote that the explicit self "is not a hack of implementation - it is a semantic device."

The reason for the explicit ā€œIā€ in the signature method of definition is the semantic consistency. If you write

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

This is really the same as writing

class C: pass

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

This was a deliberate design decision, and not the result of introducing OO behavior on the last date.

Everything in Python is an object, including literals.

See also. Why am I explicitly used in method definitions and calls?

+5
source share

All Articles