Python类访问限制

如果要让内部属性不被外部访问,可以把属性的名称前加上两个下划线__,在Python中,实例的变量名如果以__开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问

class Student(object):

    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def print_score(self):
        print('%s: %s' % (self.__name, self.__score))
        
bart = Student('Bart Simpson', 59)
bart.__name
AttributeError: 'Student' object has no attribute '__name'
    
class Student(object):

    def __init__(self, name, score):
        self.__name = name
        self.__score = score

    def print_score(self):
        print('%s: %s' % (self.__name, self.__score))
    def get_name(self):
        return self.__name
    
bart = Student('Bart Simpson', 59)

print(bart.get_name())
Bart Simpson
posted @ 2018-10-06 21:28  luoganttcc  阅读(94)  评论(0编辑  收藏  举报