Discuss / Python / 类(class)中方法(method)的定义

类(class)中方法(method)的定义

Topic source

类(class)中方法(method)定义似乎必须存在第一个参数self。

在这小节的示例Student中

    def print_score(self):
        print('%s: %s' % (self.name, self.score))

的self去掉或更换为其它变量,均报错。

没有报错

这个self是必须要有的,代表一个实例

槲寄君

#4 Created at ... [Delete] [Delete and Lock User]

同时也要修改init中的self


  • 1

Reply