python内置方法、模块讲解(二十九)

  • 2016-07-23 22:32:47
  • 1313
  • 0

七十四、super类

1、定义:

class super(object)
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type) -> unbound super object
 |  super(type, type2) -> bound super object; requires issubclass(type2, type)
 |  Typical use to call a cooperative superclass method:
 |  class C(B):
 |      def meth(self, arg):
 |          super(C, self).meth(arg)

2、解释:用于调用父类的方法,尤其适用于覆写父类该同名方法的时候

3、举例:

class FooParent(object):  
    def __init__(self):  
        self.parent = 'I\'m the parent.'  
        print 'Parent'  
      
    def bar(self,message):  
        print message,'from Parent'  
  
class FooChild(FooParent):  
    def __init__(self):  
        super(FooChild,self).__init__()  
        print 'Child'  
          
    def bar(self,message):  
        super(FooChild, self).bar(message)  
        print 'Child bar fuction'  
        print self.parent  
  
if __name__ == '__main__':  
    fooChild = FooChild()  
    fooChild.bar('HelloWorld')  
结果:
Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.

七十五、tuple类

1、定义:

class tuple(object)
 |  tuple() -> empty tuple
 |  tuple(iterable) -> tuple initialized from iterable's items
 |
 |  If the argument is a tuple, the return value is the same object.

2、解释:初始化一个元组
3、举例:

>>> c
set([1, 2])
>>> tuple(c)
(1, 2)
>>> tuple()
()
>>> tuple("1234")
('1', '2', '3', '4')
>>> tuple(["a","b"])
('a', 'b')

 


发表评论

* *