chapter9: python 类的方法,属性,迭代器(new[old]-type class,[un]bound method,super,protocol,property,iterators)
2012-09-12 17:41
786 查看
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 wrong param1 = 1 def __init__(self,value=41): self.somevalue = value def f1(self): #must have self pass t1 = TestConstructor() print t1.somevalue t2 = TestConstructor('value can be assigned too') print t2.somevalue t2.f1() print t2.param1 print 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 = True def eat(self): if self.hungry: print 'eat!!' self.hungry = False else : print 'i am full' class SongBird(Bird): # must be superClass,can use unbound method in class ##override def __init__(self): #Bird.__init__(self) # using unbound method super(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:p207create an infinite sequenceProtocol:这里的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/2137679property( [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 = None
self._y = None
def getx(self):
print '------getx'
return self._x,100,200
def setx(self, value):
print '------setx'
self._x,self._y = value
def delx(self):
print '------delx'
del self._x
xx = 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?
未完待续
相关文章推荐
- python基础教程_学习笔记11:魔法方法、属性和迭代器
- python中类的学习:类属性,实例属性 __new__方法:单例模式
- class_copyPropertyList与class_copyIvarList区别,获取属性,使用runtime方法的注意事项
- 【python初级】010-构造方法,属性和迭代器
- python中类的学习:类属性,实例属性 __new__方法:单例模式
- python类:class创建、数据方法属性及访问控制
- python基础教程_学习笔记11:魔法方法、属性和迭代器
- Python基础教程——9魔法方法、属性及迭代器【总结】
- python中7种实现单例模式的方法:staticmethod、classmethod、类属性方法、__new__、装饰器、元类、名字覆盖
- Python 里的 super() 函数和 object.__new__ ()、.__del__() 方法
- Python3基础 描述符类与自制property方法 __get__,__set__,__delete__方法 讲一个特殊的类的实例指派给另一个类的属性
- python零碎知识(6)--魔法方法、属性和迭代器
- Python基础教程——9魔法方法、属性及迭代器【总结】
- Python tricks(7) -- new-style class的__slots__属性
- Python基础教程 第9章: 魔法方法、属性和迭代器 学习笔记
- Python中的特殊方法、属性和迭代器
- Python基础教程---魔法方法,属性和迭代器(1)
- 创建Bean的三种方式 在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法。实际上,Sprin
- Python入门8_方法,属性,迭代器
- python-静态方法staticmethod、类方法classmethod、属性方法property