Discuss / Python / 为什么不需要nonlocal声明count呢,这不属于内层函数修改外层函数变量吗

为什么不需要nonlocal声明count呢,这不属于内层函数修改外层函数变量吗

Topic source

class Student_test:

    count=0

    def __init__(self,name):

        self.name=name

        Student_test.count=Student_test.count+1

廖雪峰

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

Student_test.count和count是两回事

前者是Student_test这个对象的属性,后者是本地变量


  • 1

Reply