self的用法
self的用法:class Person:def __init__(self,name):
self.name=name;
def sayhello(self):
print("My name is:",self.name);
p=Person("ysw")
p.sayhello()
注意init左右各两个下划线,不然会报错:TypeError: this constructor takes no arguments
new了一个对象p,当调用p.sayhello(),默认首先进入init函数,那么self.name = ysw;
页:
[1]