K12教育赛事综合服务平台
聚乐之家官方网站
下载聚乐之家官方App
专注青少年竞赛题库网站
已知如下Python代码:
class Student: def __init__(self, name): self.name = name def say_hello(self): return f"Hello, {self.name}" stu = Student("Alice")
可以直接通过Student.say_hello()调用该实例方法,无需额外传参
Student.say_hello()
通过实例stu调用say_hello()方法时,Python会自动将stu作为第一个参数传递给self形参
stu
say_hello()
self
实例方法say_hello定义时不需要写第一个形参,调用时也不需要传参
say_hello
若修改say_hello为def say_hello(self, msg):,则调用时需要同时传递self和msg两个参数
def say_hello(self, msg):
msg