1、类的访问限制:要让内部属性不被外部访问,可以把在属性的名称前加上两个下划线__,在Python中,实例的变量名如果以__开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问如std.__name访问报错。但可以通过std._Student__name访问

class Student():
  def __init__(self,name,score):
    self.__name = name
    self.__score = score
  def print_score(self):
    print '%s: %s' % (self.__name, self.__score)

std = Student('vickey',99)
print '111',std.Student__name
print '222',std.__name

python类访问限制

 

 

相关文章:

  • 2021-11-07
  • 2021-11-26
  • 2021-05-31
  • 2021-09-09
  • 2021-10-19
  • 2022-12-23
  • 2022-01-10
猜你喜欢
  • 2021-09-12
  • 2021-09-26
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2021-07-01
相关资源
相似解决方案