zoukankan      html  css  js  c++  java
  • python--访问限制

    访问限制

    我们可以给一个实例绑定很多属性,如果有些属性不希望被外部访问到怎么办?

    Python对属性权限的控制是通过属性名来实现的,如果一个属性由双下划线开头(__),该属性就无法被外部访问。看例子:

    class Person(object):
        def __init__(self, name):
            self.name = name
            self._title = 'Mr'
            self.__job = 'Student'
    p = Person('Bob')
    print p.name
    # => Bob
    print p._title
    # => Mr
    print p.__job
    # => Error
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Person' object has no attribute '__job'

    可见,只有以双下划线开头的"__job"不能直接被外部访问。

    但是,如果一个属性以"__xxx__"的形式定义,那它又可以被外部访问了,以"__xxx__"定义的属性在Python的类中被称为特殊属性,有很多预定义的特殊属性可以使用,通常我们不要把普通属性用"__xxx__"定义。

    以单下划线开头的属性"_xxx"虽然也可以被外部访问,但是,按照习惯,他们不应该被外部访问。

    任务

    请给Person类的__init__方法中添加name和score参数,并把score绑定到__score属性上,看看外部是否能访问到。

    代码

    class Person(object):
        def __init__(self, name, score):
            self.name = name
            self.__score = score
    
    p = Person('Bob', 59)
    
    print (p.name)
    try:
        print(p.__score)
    except AttributeError:
        print('attributeerror')

    运行结果

    Bob
    attributeerror
  • 相关阅读:
    Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    mac 上面安装mysql-python
    NSConditionLock
    NSCondition
    web.py 学习(二)Worker
    web.py 学习(-)Rocket web框架
    ARC 下面可能导致的内存问题
    WireShark 抓取Telnet包
    node.js里npm install --save 与 npm install --save-dev 的区别
    最近阅读链接
  • 原文地址:https://www.cnblogs.com/SCCQ/p/12274909.html
Copyright © 2011-2022 走看看