chapter9: python 类的方法,属性,迭代器(new[old]-type class,[un]bound method,super,protocol,property,iterators)

本文主要是介绍chapter9: python 类的方法,属性,迭代器(new[old]-type class,[un]bound method,super,protocol,property,iterators),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

chapter 9:Magic Methods, Properties, and Iterators

1 new-type class & old-type class

参考: http://ebkk.blog.163.com/blog/static/194135085200972692621216/

从python 2.2 起,如果一个类继承自object 对象(或者它是任何内建类型如list, dict, file 的子类),那它就是一个new-style class 。在此之前,Python不允许通过继承内建类型生成新类,也根本没有object 这个对象 。

# old-type 举例
#! /usr/bin/python
__metaclass__ = type 
class Person:
	defshow():	print ‘just a test!’
p = Person()
p.__init__()  # init() 是object对象的method,新类才有;如果以class Person的方式,则为old-type,则需要在最初标示__metaclass__= type,标示为new-type
# new-type 举例
#! /usr/bin/python
Class Person(object): # 内容略,和上列有同样效果

2 magic method-> constructors 构造函数

class TestConstructor(object):#self.param2 = 2 wrongparam1 = 1def __init__(self,value=41):self.somevalue = valuedef f1(self): #must have selfpass
t1 = TestConstructor()
print t1.somevaluet2 = TestConstructor('value can be assigned too')
print t2.somevalue
t2.f1()
print t2.param1print TestConstructor.param1

3 bound method /unbound method & super function

简单说:绑定的只通过实例(instance)调用,unbound只通过类(class)调用

super function 和unbound method 区别?

Super 更直观,灵活;如有多个父类时可以一次性获取,返回superclass object

class Bird(object):def __init__(self):self.hungry = Truedef eat(self):if self.hungry:print 'eat!!'self.hungry = Falseelse :print 'i am full'
class SongBird(Bird): # must be superClass,can use unbound method in class##overridedef __init__(self):#Bird.__init__(self) # using unbound methodsuper(SongBird,self).__init__() # same with upper,using super self.song = 'music time'def sing(self):print self.song
sb1 = SongBird()
sb1.eat()
sb1.sing()

4 The Basic Sequence and Mapping Protocol 

Question: p207 create an infinite sequence 
Protocol:这里的protocol类似接口等,是一种约定的实现方法

seq = [1,2,3,3]
dic = {'name':'lili','no':10}print seq.__len__()
print seq.__getitem__(2)
seq.__setitem__(2,20)
print seq.__getitem__(2)
seq.__delitem__(1) #delete seq[1] #[1,20,3]
print seq

5 property function

参考: http://blog.csdn.net/jiyucn/article/details/2137679 
property( [fget[, fset[, fdel[, doc]]]])

Return a property attribute for new-style classes (classes that derive from object). 
fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del'ing, an attribute. 

If given, doc will be the docstring of the property attribute. Otherwise, the property will copy fget's docstring (if it exists). This makes it possible to create read-only properties easily using property() as a decorator:

大概含义是,如果要使用property函数,首先定义class的时候必须是object的子类。通过property的定义,当获取成员x的值时,就会调用getx函数,当给成员x赋值时,就会调用setx函数,当删除x时,就会调用delx函数。使用属性的好处就是因为在调用函数,可以做一些检查。如果没有严格的要求,直接使用实例属性可能更方便。

class C(object):def __init__(self):self._x = Noneself._y = None def getx(self):print '------getx'return self._x,100,200def setx(self, value):print '------setx'self._x,self._y = valuedef delx(self):print '------delx'del self._xxx = property(getx, setx, delx, "I'm the 'x' property.")c1 = C()
c1._x = 10# 使用property,实现调用函数getx,只读获取return值
print c1.xx   # ------getx (10, 100 ,200)# 使用property,实现调用函数setx,赋值
c1.xx = 11,22
print c1._x,c1._y  # ------setx 11 22  
print '--', C.xx.__doc__ # I'm the 'x' property

# 如何调用delx?  


未完待续

这篇关于chapter9: python 类的方法,属性,迭代器(new[old]-type class,[un]bound method,super,protocol,property,iterators)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610085

相关文章

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os