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

  • 2016-05-01 17:55:27
  • 1191
  • 0

四十七、list类

1、定义:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items

2、解释:生成一个list 对象或者将一个迭代对象的所有迭代元素初始化为一个list对象。

另外list对象本身还有很多对应的方法,可以使用前面说的dir函数进行查看,这里不做介绍有机会后面会讲解:

>>> dir(list())
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

3、举例:

>>> list('123asdfas')
['1', '2', '3', 'a', 's', 'd', 'f', 'a', 's']
>>> list((1,2,4,'a'))
[1, 2, 4, 'a']
>>> list()
[]

四十八、locals方法

(以下省略)

阅读更多

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

  • 2016-04-03 12:00:43
  • 1132
  • 0

四十五、iter方法

1、定义:

iter(...)
    iter(collection) -> iterator
    iter(callable, sentinel) -> iterator

    Get an iterator from an object.  In the first form, the argument must
    supply its own iterator, or be a sequence.
    In the second form, the callable is called until it returns the sentinel.

2、解释:得到一个对象的迭代器, 第一种调用方式,传入参数本身必须可迭代,或者是一个序列。

                第二张调用方式, callable会被一直调用直到返回与sentinel一样的内容。

(以下省略)

阅读更多

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

  • 2016-03-15 22:08:39
  • 1137
  • 0

四十三、isinstance方法

1、定义:

isinstance(...)
    isinstance(object, class-or-type-or-tuple) -> bool

    Return whether an object is an instance of a class or of a subclass thereof.    With a type as second argument, return whether that is the object's type.
    The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
    isinstance(x, A) or isinstance(x, B) or ... (etc.).

2、解释:返回bool类型。用来判断一个对象是否是一个类的实例或者子类的实例,或者判断是否为某种对象类型。

3、举例:

>>> isinstance('aa',basestring)
True
>>> isinstance(u'aa',basestring)
True
>>> isinstance(1,(basestring,int))
True
>>> isinstance('aa',str)
True
>>> isinstance(u'aa',str)
False

四十四、issubclass方法

(以下省略)

阅读更多