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.
我已经完成了一些Python但刚刚开始使用Ruby我可以使用这两种语言中“self”之间差异的一个很好的解释。
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.
乍看之下显而易见:Self不是Python中的关键词,但无论你怎么称呼它都有一个“自我喜欢”的价值。 Python方法将self视为显式参数,而Ruby则不然。 Ruby有时使用点表示法将方法明确定义为self的一部分。
Initial Googling reveals
http://rubylearning.com/satishtalim/ruby_self.html
http://www.ibiblio.org/g2swap/byteofpython/read/self.html
最初的谷歌搜索显示http://rubylearning.com/satishtalim/ruby_self.html http://www.ibiblio.org/g2swap/byteofpython/read/self.html
4 个解决方案
#1
7
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.
Python旨在支持不仅仅是面向对象的编程。保留方法和函数之间的相同接口可以使两种样式更干净地互操作。
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.
Ruby是从头开始构建的,面向对象。甚至文字都是对象(评估1.class,你得到Fixnum)。构建语言使self成为保留关键字,无论您身在何处都返回当前实例。
If you're inside an instance method of one of your class, self is a reference to said instance.
如果你在一个类的实例方法中,self就是对所述实例的引用。
If you're in the definition of the class itself (not in a method), self is the class itself:
如果你在类本身的定义中(不在方法中),self就是类本身:
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.
在课程定义时,将打印“我是C”。
The straight 'def' defines an instance method, whereas the 'def self.xxx' defines a class method.
直接'def'定义了一个实例方法,而'def self.xxx'定义了一个类方法。
c=C.new
c.instance_method
#=> instance_method
C.class_method
#=> class_method
#2
5
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.
self仅作为惯例使用,您可以使用垃圾邮件,培根或香肠代替自己,并获得相同的结果。它只是传递给绑定方法的第一个参数。但坚持使用自我,因为它会混淆他人和一些编辑。
#3
5
Despite webmat's claim, Guido wrote that explicit self is "not an implementation hack -- it is a semantic device".
尽管网络媒体声称,Guido写道,明确的自我“不是实施黑客 - 它是一种语义设备”。
The reason for explicit self in method definition signatures is semantic consistency. If you write
在方法定义签名中显式自我的原因是语义一致性。如果你写
class C: def foo(self, x, y): ...
C级:def foo(self,x,y):...
This really is the same as writing
这与写作完全一样
class C: pass
C级:通过
def foo(self, x, y): ... C.foo = foo
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.
这是一个有意的设计决定,而不是在后一天引入OO行为的结果。
Everything in Python -is- an object, including literals.
Python中的所有东西都是一个对象,包括文字。
See also Why must 'self' be used explicitly in method definitions and calls?
另请参阅为什么必须在方法定义和调用中明确使用“self”?
#4
5
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.
好吧,我对Ruby不太了解。但是关于Python“自我”的明显之处在于它不是一个“关键字”......它只是发送给你的方法的参数的名称。
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.
打印终端上的数字6。在构造函数中,self对象实际上称为a。但是在p()方法中,它被称为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
更新:2008年10月,Guido指出,拥有一个明确的自我也是必要的,以允许Python装饰器足够通用,可以处理纯函数,方法或类方法:http://neopythonic.blogspot.com/2008/10/why -explicit自具有对stay.html
#1
7
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.
Python旨在支持不仅仅是面向对象的编程。保留方法和函数之间的相同接口可以使两种样式更干净地互操作。
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.
Ruby是从头开始构建的,面向对象。甚至文字都是对象(评估1.class,你得到Fixnum)。构建语言使self成为保留关键字,无论您身在何处都返回当前实例。
If you're inside an instance method of one of your class, self is a reference to said instance.
如果你在一个类的实例方法中,self就是对所述实例的引用。
If you're in the definition of the class itself (not in a method), self is the class itself:
如果你在类本身的定义中(不在方法中),self就是类本身:
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.
在课程定义时,将打印“我是C”。
The straight 'def' defines an instance method, whereas the 'def self.xxx' defines a class method.
直接'def'定义了一个实例方法,而'def self.xxx'定义了一个类方法。
c=C.new
c.instance_method
#=> instance_method
C.class_method
#=> class_method
#2
5
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.
self仅作为惯例使用,您可以使用垃圾邮件,培根或香肠代替自己,并获得相同的结果。它只是传递给绑定方法的第一个参数。但坚持使用自我,因为它会混淆他人和一些编辑。
#3
5
Despite webmat's claim, Guido wrote that explicit self is "not an implementation hack -- it is a semantic device".
尽管网络媒体声称,Guido写道,明确的自我“不是实施黑客 - 它是一种语义设备”。
The reason for explicit self in method definition signatures is semantic consistency. If you write
在方法定义签名中显式自我的原因是语义一致性。如果你写
class C: def foo(self, x, y): ...
C级:def foo(self,x,y):...
This really is the same as writing
这与写作完全一样
class C: pass
C级:通过
def foo(self, x, y): ... C.foo = foo
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.
这是一个有意的设计决定,而不是在后一天引入OO行为的结果。
Everything in Python -is- an object, including literals.
Python中的所有东西都是一个对象,包括文字。
See also Why must 'self' be used explicitly in method definitions and calls?
另请参阅为什么必须在方法定义和调用中明确使用“self”?
#4
5
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.
好吧,我对Ruby不太了解。但是关于Python“自我”的明显之处在于它不是一个“关键字”......它只是发送给你的方法的参数的名称。
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.
打印终端上的数字6。在构造函数中,self对象实际上称为a。但是在p()方法中,它被称为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
更新:2008年10月,Guido指出,拥有一个明确的自我也是必要的,以允许Python装饰器足够通用,可以处理纯函数,方法或类方法:http://neopythonic.blogspot.com/2008/10/why -explicit自具有对stay.html