python __slots__使用详解
1.动态添加属性
class Lang(object):
def __init__(self,name,score):
self.name=name
self.score=score
def langinfo(self):
print '%s:%s'%(self.name,self.score)
lang1=Lang('Python','8.5')
lang1.rank=4
print lang1.rank
运行结果:

2.动态添加方法
from types import MethodType
class Lang(object):
def __init__(self,name,score):
self.name=name
self.score=score
def langinfo(self):
print '%s:%s'%(self.name,self.score)
lang1=Lang('Python','8.5')
def getrank(self):
return 4
lang1.getrank=MethodType(getrank,lang1,Lang)
print lang1.getrank()
运行结果:

这种方法只是给实例lang1,动态添加了方法
from types import MethodType
class Lang(object):
def __init__(self,name,score):
self.name=name
self.score=score
def langinfo(self):
print '%s:%s'%(self.name,self.score)
lang1=Lang('Python','8.5')
lang2=Lang('C','')
def getrank(self):
return 4
lang1.getrank=MethodType(getrank,lang1,Lang)
print lang2.getrank()
运行结果:

给类添加方法:
from types import MethodType
class Lang(object):
def __init__(self,name,score):
self.name=name
self.score=score
def langinfo(self):
print '%s:%s'%(self.name,self.score)
lang1=Lang('Python','8.5')
lang2=Lang('C','')
def getrank(self):
return 4
Lang.getrank=MethodType(getrank,None,Lang)
print lang2.getrank()
运行结果:

3.限制Class属性 __slots__
#__slots__使用
from types import MethodType
class Lang(object):
__slots__=('name','score','rank')
def __init__(self,name,score):
self.name=name
self.score=score
def langinfo(self):
print '%s:%s'%(self.name,self.score)
lang1=Lang('Python','8.5')
lang1.rank=4
lang1.desc='Simple'
print lang1.rank
运行结果:

python __slots__使用详解的更多相关文章
- (转)python collections模块详解
python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
- python time模块详解
python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明 一.简介 ...
- Python中dict详解
from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...
- Python开发技术详解(视频+源码+文档)
Python, 是一种面向对象.直译式计算机程序设计语言.Python语法简捷而清晰,具有丰富和强大的类库.它常被昵称为胶水语言,它能够很轻松的把用其他语言制作的各种模块(尤其是C/C++)轻松地联结 ...
- python/ORM操作详解
一.python/ORM操作详解 ===================增==================== models.UserInfo.objects.create(title='alex ...
- 【python进阶】详解元类及其应用2
前言 在上一篇文章[python进阶]详解元类及其应用1中,我们提到了关于元类的一些前置知识,介绍了类对象,动态创建类,使用type创建类,这一节我们将继续接着上文来讲~~~ 5.使⽤type创建带有 ...
- Python开发技术详解PDF
Python开发技术详解(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1F5J9mFfHKgwhkC5KuPd0Pw 提取码:xxy3 复制这段内容后打开百度网盘手 ...
- python之数据类型详解
python之数据类型详解 二.列表list (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...
随机推荐
- IE hack 汇总
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- nexus 离线更新索引
1.到http://repo.maven.apache.org/maven2/.index/页面下载下面这两个文件: nexus-maven-repository-index.gz nexus-mav ...
- Memcached安装与配置
memcached是danga.com的一个项目.它是一款开源的高性能的分布式内存对象缓存系统.最早是给LiveJournal提供服务的.后来逐渐被越来越多的大型站点所採用.用于在应用中减少对数据库 ...
- 〖Linux〗VIM youcompleteme 自动补全 #include 文件名称
1. 拷贝配置文件 cp ~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py ~/.vim/.ycm_extra_conf.py 2. 修改配 ...
- DevExpress.XtraLayout.LayoutControl 动态添加控件
// Create an item within a specified group,// bound to a specified data field with the specified edi ...
- Android网络开发之HttpClient
Apache提供HttpClient,它对java.net中的类做了封装和抽象,更适合在Android上开发应用. HttpClient应用开发几个类: 1. ClientConnectionMana ...
- js实现new Date(),时间对象和时间戳操作
1.js中实现时间date对象 var myDate = new Date();//获取系统当前时间,结果:Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) 2.获 ...
- VC++6.0 IDE的工程用Code::Blocks来打开、编译、调试终极配置方案
本篇文章转自 CSDN blog,转载请注明出处! 原文地址: http://blog.csdn.net/liquanhai/article/details/6618300 引子:竟然可以用Code: ...
- mybatis启动报错Result Maps collection already contains value forxxx
ssm搭建过程中启动tomcat,报错: Cause: java.lang.IllegalArgumentException: Result Maps collection already conta ...
- 常见Web前端开发笔试题
1.什么是web标准? WEB标准不是某一个标准,而是一系列标准的集合.网页主要由三部分组成:结构(Structure).表现(Presentation)和行为 (Behavior). 对应的标准也分 ...