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) -->['!', ' ...
随机推荐
- HDUOj Ignatius and the Princess III 题目1002
母函数 组合数学 #include<stdio.h> int c1[125]; int c2[125]; int main() { int n,i,j,k; while(scanf ...
- 使用Fiddler抓包调试https下的页面
众所周知https技术诞生以来,一个很重要的作用就是加密通信内容.所以在项目团队将业务站点实施完https改造以后,原先使用fiddler进行抓包的美好生活到头了.其实fiddler本身是支持对于ht ...
- Mount 挂载错误mount:block device /dev/sr0 is write – protected , mounting read-only
Mount 挂载错误mount:block device /dev/sr0 is write – protected , mounting read-only 安装虚拟机出现以下提示: mount:b ...
- 【Android开发经验】Cannot generate texture from bitmap异常的解决方式
异常现象: 今天在处理用户头像的过程中,由于头像的处理比較复杂,由于,没有使用afinal自带的自己主动载入.而是自己依据头像的下载路径.手动进行下载和使用.可是在手动回收bitmap对象的过程中,会 ...
- 【Oracle】事务处理
名词解释 DML:Data Manipulation Language (数据库操纵语言) 例如:DELETE.INSERT.UPDATE.SELECT DDL:Data Definition Lan ...
- 在shell中使用sed命令替换/为\/
sed命令相关: https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html https://www.cnblogs.com/D ...
- fdisk -l解析
fdisk -l显示信息详解 [root@www.linuxidc.com ~]# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 hea ...
- HighCharts终极版本
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- IBM InfoSphere DataStage 8.1 DataStage Job 开发具体解释
简单介绍 DataStage 使用了 Client-Server 架构,server端存储全部的项目和元数据,client DataStage Designer 为整个 ETL 过程提供了一个图形化的 ...
- Android API之android.provider.ContactsContract.Contacts
android.provider.ContactsContract.Contacts 对应contacts数据表.RawContacts的一个聚合(aggregate)代表同一个人.每个人在数据表co ...