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

  • 2016-02-21 23:02:52
  • 1334
  • 0

四、apply方法

1、定义:      

apply(...)
    apply(object[, args[, kwargs]]) -> value

    Call a callable object with positional arguments taken from the tuple args,
    and keyword arguments taken from the optional dictionary kwargs.
    Note that classes are callable, as are instances with a __call__() method.

    Deprecated since release 2.3. Instead, use the extended call syntax:
        function(*args, **keywords).

2、解释:​根据有序的元组、字典调用一个可调用的对象, 注:拥有__call__()方法的实例是可调用的, 生成实例的类也是有可调用的。

​3、举​例:

>>> def test(a, b):
...     print a, b
...
>>> apply(test, ('hello', 'world'))
hello world

五、basestring类型

​1、定义:

class basestring(object)
 |  Type basestring cannot be instantiated; it is the base for str and unicode.

2、解释:basestring类型不能实例化, 是str和unicode的基类。即str、unicode类型都属于basestring 类型。

3、举例:

>>> isinstance('',basestring)
True
>>> isinstance(u'',basestring)
True

六、bin方法

1、定义:

bin(...)
    bin(number) -> string

    Return the binary representation of an integer or long integer.

2、解释:将一个number转变成整型或者长整型的二进制表示。

3、举例:

>>> bin(2)
'0b10'
>>> bin(1)
'0b1'
>>> bin(3)
'0b11'
>>> bin(4)
'0b100'
>>> bin(0)
'0b0'
注:0b代表二进制

七、bool方法

1、定义:

class bool(int)
 |  bool(x) -> bool
 |
 |  Returns True when the argument x is true, False otherwise.
 |  The builtins True and False are the only two instances of the class bool.
 |  The class bool is a subclass of the class int, and cannot be subclassed.

2、解释:输入参数为x, 如果x为真, 则返回True,否则返回False。

3、举例:

>>> bool('')
False
>>> bool('1')
True
>>> bool(0)
False
>>> bool(1)
True

 


发表评论

* *