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__使用详解的更多相关文章

  1. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  2. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息.        ...

  3. python time模块详解

    python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明  一.简介 ...

  4. Python中dict详解

    from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...

  5. Python开发技术详解(视频+源码+文档)

    Python, 是一种面向对象.直译式计算机程序设计语言.Python语法简捷而清晰,具有丰富和强大的类库.它常被昵称为胶水语言,它能够很轻松的把用其他语言制作的各种模块(尤其是C/C++)轻松地联结 ...

  6. python/ORM操作详解

    一.python/ORM操作详解 ===================增==================== models.UserInfo.objects.create(title='alex ...

  7. 【python进阶】详解元类及其应用2

    前言 在上一篇文章[python进阶]详解元类及其应用1中,我们提到了关于元类的一些前置知识,介绍了类对象,动态创建类,使用type创建类,这一节我们将继续接着上文来讲~~~ 5.使⽤type创建带有 ...

  8. Python开发技术详解PDF

    Python开发技术详解(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1F5J9mFfHKgwhkC5KuPd0Pw 提取码:xxy3 复制这段内容后打开百度网盘手 ...

  9. python之数据类型详解

    python之数据类型详解 二.列表list  (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...

随机推荐

  1. Linux rpm安装MySQL

    1:查看操作系统信息 ##uname -a : 准备软件包: MySQL-server-5.6.19-1.rhel5.x86_64.rpm MySQL-devel-5.6.19-1.rhel5.x86 ...

  2. spring 中常用的配置项

    1.spring 中常用的配置项 application.properties #端口 server.port=8081 #调试模式 debug=false #上下文 #一般情况下,小项目通常都是在t ...

  3. Vue 生命周期LIFECYCLE是8个吗?

    vue生命周期钩子个数是:11个. export const LIFECYCLE_HOOKS = [ 'beforeCreate', 'created', 'beforeMount', 'mounte ...

  4. rarcrack

    apt-get install rarcrack使用方法:rarcrack --threads xx --type rar xx.rar

  5. R语言中数据结构

    R语言还是有点古老感觉,数据结构没有Python中那么好用.以下简单总结一下R语言中经常使用的几个数据结构. 向量: R中的向量能够理解为一维的数组,每一个元素的mode必须同样,能够用c(x:y)进 ...

  6. eclipse 创建mavenWeb项目

      eclipse 创建mavenWeb项目 CreationTime--2018年6月7日18点46分 Author:Marydon 一.创建MavenWeb项目 1.右键-->New--&g ...

  7. 1、创建一个空白的xls和xlsx文件

    1.创建一个空白的xls文件 Step1:先引入库NPOI.dll文件 Step2: ①:实例化一个workbook,实为在内存表中创建一个xls文件 NPOI.HSSF.UserModel.HSSF ...

  8. java Socket Tcp示例三则(服务端处理数据、上传文件)

    示例一: package cn.itcast.net.p5.tcptest; import java.io.BufferedReader;import java.io.IOException;impo ...

  9. 微信小程序“信用卡还款”项目实践

    小程序概述 11月3日晚,微信团队对外宣布,微信小程序开放公测.开发者可登陆微信公众平台申请,开发完成后可以提交审核,公测期间暂不能发布. 我们前一段时间也进行了小程序开发,现在来对之前的开发体验做一 ...

  10. Matlab调用返回游标的存储过程的分析和处理

    2.Matlab调用Oracl带游标参数输出的存储过程 笔者也是将工作之中遇到的问题进行了搜集与整理,才完成该文的编写,希望能帮助到有需要的朋友. 2.1.PLSQL中的存储过程 PROCEDURE ...