zoukankan      html  css  js  c++  java
  • python3 高级编程(一) 使用__slots__

    使用__slots__的目的:限制实例的属性

    用法:定义class的时候,定义一个特殊的__solts__变量,来限制实例能添加的属性。

    class Student(object):
        __slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称
    
    >>> s = Student() # 创建新的实例
    >>> s.name = 'Michael' # 绑定属性'name'
    >>> s.age = 25 # 绑定属性'age'
    >>> s.score = 99 # 绑定属性'score'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Student' object has no attribute 'score'

    由于'score'没有被放到__slots__中,所以不能绑定score属性,试图绑定score将得到AttributeError的错误。

    使用__slots__要注意,__slots__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的:

    除非在子类中也定义__slots__,这样,子类实例允许定义的属性就是自身的__slots__加上父类的__slots__

    >>> class GraduateStudent(Student):
    ...     pass
    ...
    >>> g = GraduateStudent()
    >>> g.score = 9999
  • 相关阅读:
    0-J2EE
    3-Spring
    linux部分常用命令
    linux配置bond
    免密登录和配置网卡
    配置网卡的子接口
    mysqldump备份
    python的数据结构
    mysql一主一从复制
    Python3 基本数据类型和类型转换
  • 原文地址:https://www.cnblogs.com/AndyChen2015/p/11388625.html
Copyright © 2011-2022 走看看