请问为什么红框内代码会报错?(语言-python)

img

img

请问红框内代码为什么会报错?dbjebdjebdjjennskdnzhgavwuhwbkdkdknenx

因为stu_no是类的成员变量,不是成员函数,后面不需要加括号“()”。
正确写法修改如下:

stu.stu_no

测试的完整代码如下:

class Person(object):
    '''
    a class Person
    '''
    def __init__(self,name='',age=20,sex='man'):
        self.setName(name)
        self.setAge(age)
        self.setSex(sex)

    def setName(self,name):
        if not isinstance(name,str):
            raise Exception('name must be a string.')
        self._name=name

    def setAge(self,age):
        if type(age)!=int:
            raise Exception('age must be an integer.')
        self._age=age

    def setSex(self,sex):
        if sex not in ('man','woman'):
            raise Exception('sex must be "man" or "woman"')
        self._sex=sex

    def show(self):
        print(self._name, self._age, self._sex, sep='\n')
 
class Student(Person):
    '''
    a class Student
    '''    
    def __init__(self,name='test',age=30, sex='man', stu_no='20220001'):
        super().__init__(name,age,sex)
        self._stu_no = stu_no

    def setNo(self, stu_no):
        if not isinstance(stu_no, str):
            raise Exception('stu_no must be a string.')
        self._stu_no=stu_no

    def show(self):
        super().show()
        print(self._stu_no)

if __name__=="__main__":
    stu=Student("Lily", 25, 'woman', '20220016')
    stu.setNo('20220055')
    print(stu._stu_no)
    stu._stu_no = '20220088'
    print(stu._stu_no)
    stu.show()

stu_no是个实例属性,它不是函数,不用括号
只有调用函数执行的时候才加括号