在Python中定义类的方法时,它看起来像这样:

class MyClass(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

但是在其他一些语言中,比如c#,你有一个对象的引用,这个对象是用"this"关键字绑定的,而不是在方法原型中作为参数声明的。

这是Python中有意的语言设计决策,还是有一些实现细节需要传递“self”作为参数?


当前回答

Python不会强制你使用"self"。你可以给它起任何你想要的名字。你只需要记住方法定义头中的第一个参数是对对象的引用。

其他回答

我认为这和PEP 227有关

Names in class scope are not accessible. Names are resolved in the innermost enclosing function scope. If a class definition occurs in a chain of nested scopes, the resolution process skips class definitions. This rule prevents odd interactions between class attributes and local variable access. If a name binding operation occurs in a class definition, it creates an attribute on the resulting class object. To access this variable in a method, or in a function nested within a method, an attribute reference must be used, either via self or via the class name.

我建议大家应该读读Guido van Rossum关于这个话题的博客——为什么外显的自我必须留下来。

When a method definition is decorated, we don't know whether to automatically give it a 'self' parameter or not: the decorator could turn the function into a static method (which has no 'self'), or a class method (which has a funny kind of self that refers to a class instead of an instance), or it could do something completely different (it's trivial to write a decorator that implements '@classmethod' or '@staticmethod' in pure Python). There's no way without knowing what the decorator does whether to endow the method being defined with an implicit 'self' argument or not. I reject hacks like special-casing '@classmethod' and '@staticmethod'.

self形参保存当前调用对象。

class class_name:
    class_variable
    def method_name(self,arg):
        self.var=arg 
obj=class_name()
obj.method_name()

在这里,self参数保存对象obj。因此,语句self。Var表示obj.var

正如Python中的self所解释的那样,Demystified

任何类似obj.meth(args)的东西都会变成Class。甲安菲他明(obj, args)。调用进程是自动的,而接收进程不是(它的显式)。这就是类中函数的第一个形参必须是对象本身的原因。

class Point(object):
    def __init__(self,x = 0,y = 0):
        self.x = x
        self.y = y

    def distance(self):
        """Find distance from origin"""
        return (self.x**2 + self.y**2) ** 0.5

调用:

>>> p1 = Point(6,8)
>>> p1.distance()
10.0

init()定义了三个参数,但我们只传递了两个(6和8)。同样,distance()需要一个参数,但传递了0个参数。

为什么Python没有抱怨这个参数号不匹配?

Generally, when we call a method with some arguments, the corresponding class function is called by placing the method's object before the first argument. So, anything like obj.meth(args) becomes Class.meth(obj, args). The calling process is automatic while the receiving process is not (its explicit). This is the reason the first parameter of a function in class must be the object itself. Writing this parameter as self is merely a convention. It is not a keyword and has no special meaning in Python. We could use other names (like this) but I strongly suggest you not to. Using names other than self is frowned upon by most developers and degrades the readability of the code ("Readability counts"). ... In, the first example self.x is an instance attribute whereas x is a local variable. They are not the same and lie in different namespaces.

自我在这里停留 许多人建议在Python中使self成为关键字,就像在c++和Java中那样。这将消除方法中形式参数列表中显式self的冗余使用。虽然这个想法看起来很有希望,但它不会发生。至少近期不会。主要原因是向后兼容。下面是Python创建者自己的一篇博客,解释了为什么显式自我必须保留。

Python不会强制你使用"self"。你可以给它起任何你想要的名字。你只需要记住方法定义头中的第一个参数是对对象的引用。