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

  • 2016-07-23 22:47:03
  • 1322
  • 0

七十六、type类

1、定义:

class type(object)
 |  type(object) -> the object's type
 |  type(name, bases, dict) -> a new type

2、解释:用于查看一个对象的类型(非常常用)或者创造一个新的类对象(可以理解成用于创造类对象(类也是对象)的元类)。

3、举例:

>>> type(1)
<type 'int'>
>>> type("a")
<type 'str'>
>>> type([])
<type 'list'>

>>> class A(object):
...     pass
...
>>> A
<class '__main__.A'>
>>> type('A', (), {})
<class '__main__.A'>

七十七、unichr方法

(以下省略)

阅读更多

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

  • 2016-07-23 22:32:47
  • 1332
  • 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
  • 1328
  • 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表现方式

(以下省略)

阅读更多