python学习-slots
来源:互联网 发布:北京学软件测试 编辑:程序博客网 时间:2024/06/10 12:37
#!/usr/bin/env python#-*- coding:utf-8 -*-'slots'__author__ = 'hui.qian''''之前一章给类动态的增加属性,现在给类动态的增加方法'''class Student(object): passs = Student()s.name = 'hui.qian'print s.name#首先定义一个方法def set_age(self,age): self.age = agefrom types import MethodTypes.set_age = MethodType(set_age,s,Student)s.set_age(25)print s.age#动态添加成功,如果这个时候我创建一个Student的实例#还能用set_age()方法么#s1 = Student()#print s1.set_age(25)#上面的调用会报错,说明刚才的添加方法只是暂时的#如果想让其他实例也拥有这样的方法,定义如下def set_score(self,score): self.score = scoreStudent.set_score = MethodType(set_score,None,Student)s2 = Student()s3 = Student()s2.set_score(99)print s2.scores3.set_score(100)print s3.score'''那么再来说说__slots__有什么作用:像上面的给类添加属性的方式,应该是随意添加一个属性但是有人就不希望你随意添加,他就得使用__slots__来标识哪些属性可以添加'''class Student(object): __slots__ = ('name','age')s = Student()s.name='hui.qian'print s.names.age=25print s.age#上面的赋值都是正确的,但是下面的赋值就会报错#s.score = 55#因为score不在__slots__中'''__slots__仅对当前类有用,对于没有定义__slots__的子类不起作用如果子类定义了__slots__,那么其作用的关键字就是父类和子类的__slots__的合集'''
1 0
- python学习-slots
- python slots初探
- 【python】面向高级对象 slots
- BlackBerry10 学习Signals和Slots
- Python内存优化:Profile,slots,compact dic
- Python 之 高级面向对象编程 slots
- Signals and Slots学习笔记(翻译)
- PyQT信号槽_学习笔记 (Signals/Slots)
- PyQT信号槽_学习笔记 (Signals/Slots)
- PyQT信号槽_学习笔记 (Signals/Slots)
- Qt public slots;protected slots;private slots
- Signals & Slots
- python 的一些知识点(列表解析、打包、装饰器、type、slots)
- 细说python类2——类动态添加方法和slots
- Qt学习笔记1--更高级Signals and Slots的用法
- python入门笔记(Day7)--slots,@property,MixIn,定制(str,iter,getitem,getattr,call)枚举(Enum)元type,metaclass,ORM
- Signals and Slots 翻译
- Private implementation slots
- HDU 1051 wooden sticks
- UVA 305 - Joseph(模拟)
- Word2007转PDF
- 自动加载函数库与扩展函数库定义
- 使用LDAPAdmin连接OpenLDAP
- python学习-slots
- 阶乘因式分解(一)
- usaco3.3.4亚瑟王的宫殿
- 函数式语言学院派之haskell之一
- quote symbol expected 异常处理
- PHP中使用cURL实现Get和Post请求的方法
- Centos6.5安装OpenLDAP
- python学习-property
- request和response中一些方法的区别