Discuss / Python / 引用类的属性,需在前面加上类名.

引用类的属性,需在前面加上类名.

Topic source

飞侠100

#1 Created at ... [Delete] [Delete and Lock User]
count = 100

class Student(object):
    count = 0
    def __init__(self,name):
        self.name = name
        print(count)         #引用了外部变量
        print(Student.count) #引用类的属性
        Student.count += 1   #对类的属性进行操作

  • 1

Reply