标签:init attribute toc 效果 object 函数 完全 就会 使用
class Student:
school = 'hnnu'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.sex = gender
def choose_course(self):
print(f'{self.name} choosing course')
def func(self):
print('from func')
stu1 = Student('randy', 18, 'male')
stu2 = Student('sun', 17, 'male')
stu3 = Student('laowang', 19, 'female')
print(stu1.name)
print(stu1.school)
randy
hnnu
print(Student.choose_course)
<function Student.choose_course at 0x10558e840>
try:
Student.choose_course(123)
except Exception as e:
print(e)
'int' object has no attribute 'name'
print(id(stu1.choose_course))
print(id(stu2.choose_course))
print(id(stu3.choose_course))
print(id(Student.choose_course))
4379911304
4379911304
4379911304
4384680000
print(id(stu1.school))
print(id(stu2.school))
print(id(stu3.school))
4380883688
4380883688
4380883688
print(id(stu1.name), id(stu2.name), id(stu3.name))
4384509600 4384506072 4384507864
stu1.choose_course()
nick choosing course
stu2.choose_course()
sean choosing course
stu3.choose_course()
tank choosing course
stu1.func()
from func
stu2.func()
from func
标签:init attribute toc 效果 object 函数 完全 就会 使用
原文地址:https://www.cnblogs.com/randysun/p/12248008.html