想想这个例子:

class MyClass:
    def func(self, name):
        self.name = name

我知道self引用MyClass的特定实例。但是为什么func必须显式地包含self作为参数呢?为什么我们需要在方法的代码中使用self ?其他一些语言将其隐式化,或者使用特殊的语法。


有关设计决策的语言无关性考虑,请参见强制显式使用this/self指针的好处是什么?

要关闭OP省略方法的self形参并获得TypeError的调试问题,请使用TypeError: method()接受1个位置参数,但给出了2个。如果OP省略了self。在方法体中得到一个NameError,考虑如何在类中调用函数?


当前回答

与Java或c++不同,Python不是为面向对象编程而构建的语言。

在Python中调用静态方法时,只需编写一个内部带有常规参数的方法。

class Animal():
    def staticMethod():
        print "This is a static method"

然而,一个对象方法,它需要你创建一个变量,在这里是Animal,它需要self参数

class Animal():
    def objectMethod(self):
        print "This is an object method which needs an instance of a class"

self方法还用于引用类中的变量字段。

class Animal():
    #animalName made in constructor
    def Animal(self):
        self.animalName = "";


    def getAnimalName(self):
        return self.animalName

在本例中,self引用了整个类的animalName变量。记住:如果你在一个方法中有一个变量,self将不起作用。该变量仅在该方法运行时存在。为了定义字段(整个类的变量),您必须在类方法之外定义它们。

如果你一个字都听不懂我在说什么,那么谷歌“面向对象编程”。一旦你明白了这一点,你甚至不需要问那个问题:)。

其他回答

与前面提到的所有其他原因一样,它允许更容易地访问被覆盖的方法;你可以调用Class.some_method(inst)。

举个例子说明它的用处:

class C1(object):
    def __init__(self):
         print "C1 init"

class C2(C1):
    def __init__(self): #overrides C1.__init__
        print "C2 init"
        C1.__init__(self) #but we still want C1 to init the class too
>>> C2()
"C2 init"
"C1 init"

我的小钱

在Person类中,我们用self定义了init方法,这里需要注意的有趣的事情是self和实例变量p的内存位置是相同的<__main__。位于0x106a78fd0>的Person对象

class Person:

    def __init__(self, name, age):
        self.name = name 
        self.age = age 

    def say_hi(self):
        print("the self is at:", self)
        print((f"hey there, my name is {self.name} and I am {self.age} years old"))

    def say_bye(self):
        print("the self is at:", self)
        print(f"good to see you {self.name}")

p = Person("john", 78)
print("the p is at",p)
p.say_hi()  
p.say_bye() 

如上所述,self和实例变量都是同一个对象。

我很惊讶居然没人提起Lua。Lua也使用“self”变量,但是可以省略,但仍然使用。c++对'this'做了同样的处理。我不认为有任何理由必须在每个函数中声明“self”,但你仍然可以像使用lua和c++一样使用它。对于一门以简洁为傲的语言来说,它要求你声明self变量是很奇怪的。

这是为了遵循Python的禅宗“显式优于隐式”。它实际上是对类对象的引用。例如,在Java和PHP中,它被称为this。

如果user_type_name是你的模型中的一个字段,你可以通过self.user_type_name访问它。

看看下面的例子,它清楚地解释了self的目的

class Restaurant(object):  
    bankrupt = False

    def open_branch(self):
        if not self.bankrupt:
           print("branch opened")

#create instance1
>>> x = Restaurant()
>>> x.bankrupt
False

#create instance2
>>> y = Restaurant()
>>> y.bankrupt = True   
>>> y.bankrupt
True

>>> x.bankrupt
False  

Self用于/需要用于区分实例。

来源:python中的self变量解释- Pythontips