slots
class People(object):
__slots__ = ("name", "age")
def __init__(self, name, age):
self.name = name
self.age = age
self.gender = 1
if __name__ == '__main__':
people = People("name", "20")
限制class的属性:上面的类中只能有属性name 和 age 而不能有gender
Traceback (most recent call last):
File "D:/Project/PycharmProject/PythonProject/learn/oo_demo.py", line 11, in <module>
people = People("name", "20")
File "D:/Project/PycharmProject/PythonProject/learn/oo_demo.py", line 7, in __init__
self.gender = 1
AttributeError: 'People' object has no attribute 'gender'
all
是一个字符串list
- 在__init__.py 文件文件中,限制package模糊导入的模块
- 在模块.py文件中,限制模块模糊导入的类和方法
- 只对import *起作用,对具体的from XXX import XXX不起作用。