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

  • 2016-07-23 22:32:47
  • 1340
  • 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')

 

阅读更多

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

  • 2016-07-03 18:38:00
  • 1339
  • 0

 

七十二、str类

1、定义:

class str(basestring)
 |  str(object='') -> string
 |
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.
 |
 |  Method resolution order:
 |      str
 |      basestring
 |      object

2、解释:返回一个对象的string表现方式

(以下省略)

阅读更多

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

  • 2016-07-03 17:16:08
  • 1160
  • 0

 

七十、sorted方法

1、定义:

sorted(...)
    sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

2、解释:生成一个整理好的list。其中iterable为可迭代对象,cmp为用来排序的比较函数, key为用来排序的关键值,reversed为是否倒序。

(以下省略)

阅读更多