请把下面的Student对象的gender字段对外隐藏起来,用get_gender()和set_gender()代替,并检查参数有效性:

class Student(object):
    def __init__(self, name, gender):
        self.name = name
        self.__gender = gender    #让gender属性对外部隐藏
    def get_gender(self):
        return self.__gender         
    def set_gender(self, gender):
        if gender == 'male' or gender == 'female':     #对gender的参数进行有效性判断
            self.__gender = gender
        else:
            raise ValueError('incorrect gender')

你可能感兴趣的:(python廖雪峰作业,类,私有属性)