博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python学习札记(三十六) 面向对象编程 Object Oriented Program 7 __slots__
阅读量:4630 次
发布时间:2019-06-09

本文共 2225 字,大约阅读时间需要 7 分钟。

参考:

NOTE

1.动态语言灵活绑定属性及方法。

#!/usr/bin/env python3class MyClass(object):    def __init__(self):        passdef func(obj):    print(obj.name, obj.age)def main():    h = MyClass()    h.name = 'Chen'    h.age = '20'    func(h)if __name__ == '__main__':    main()

给对象h绑定了属性name和age。

sh-3.2# ./oop7.py Chen 20

绑定一个新的方法:

from types import MethodTypedef f(self):    print('I\'m new here!')h.f = MethodType(f, h) # new methodh.f()
I'm new here!

但是这种绑定的方法并不存在于新建的对象:

h1 = MyClass()    h1.f()
Traceback (most recent call last):  File "./oop7.py", line 28, in 
main() File "./oop7.py", line 25, in main h1.f()AttributeError: 'MyClass' object has no attribute 'f'

给类绑定一个方法,解决这个问题:

MyClass.f = f    h1 = MyClass()    h1.f()
I'm new here!

通常情况下,上面的f方法可以直接定义在class中,但动态绑定允许我们在程序运行的过程中动态给class加上功能,这在静态语言中很难实现。

2.__slots__

但是,如果我们想要限制实例的属性怎么办?比如,只允许对MyClass实例添加name和age属性。

为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性:

#!/usr/bin/env python3class MyClass(object):    """docstring for MyClass"""    __slots__ = ('name', 'age')    def __init__(self):        super(MyClass, self).__init__()        passdef main():    h = MyClass()    h.name = 'Chen'    h.age = 20    h.city = 'FuZhou'if __name__ == '__main__':    main()
sh-3.2# ./oop8.py Traceback (most recent call last):  File "./oop8.py", line 17, in 
main() File "./oop8.py", line 14, in main h.city = 'FuZhou'AttributeError: 'MyClass' object has no attribute 'city'

__slots__用tuple定义允许绑定的属性名称,由于'city'没有被放到__slots__中,所以不能绑定city属性。

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

#!/usr/bin/env python3class MyClass(object):    """docstring for MyClass"""    __slots__ = ('name', 'age')    def __init__(self):        super(MyClass, self).__init__()        passclass Student(MyClass):    """docstring for Student"""    def __init__(self):        super(Student, self).__init__()        pass        def main():    h = MyClass()    h.name = 'Chen'    h.age = 20    # h.city = 'FuZhou'    h1 = Student()    h1.name = 'Chen'    h1.age = 20    h1.city = 'FuZhou'    print(h1.name, h1.age, h1.city)if __name__ == '__main__':    main()
sh-3.2# ./oop8.py Chen 20 FuZhou

2017/3/2

转载于:https://www.cnblogs.com/qq952693358/p/6493135.html

你可能感兴趣的文章
[C++]C++中的IO类
查看>>
笔记本电脑(Windows7)实现无线AP
查看>>
JqGridView 1.0.0.0发布
查看>>
欲精一行,必先通十行
查看>>
前端相关html和css
查看>>
celery
查看>>
实现音乐播放器
查看>>
BZOJ1002 [FJOI2007]轮状病毒(最小生成树计数)
查看>>
uv_timer_t的释放问题
查看>>
【bzoj1853】[Scoi2010]幸运数字 容斥原理+搜索
查看>>
【bzoj2770】YY的Treap 权值线段树
查看>>
BZOJ1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
查看>>
Shell基础命令之echo
查看>>
windows 常用命令
查看>>
python中tornado的第一个例子
查看>>
分享下自己写的一个微信小程序请求远程数据加载到页面的代码
查看>>
微软技术的变迁
查看>>
从网络上获取一张图片简单的
查看>>
大道至简第一章读后感
查看>>
迷宫寻宝(搜索)
查看>>