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

  • 2016-03-14 22:35:53
  • 1272
  • 0

四十一、int类

1、定义:

class int(object)
 |  int(x=0) -> int or long
 |  int(x, base=10) -> int or long
 |
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is floating point, the conversion truncates towards zero.
 |  If x is outside the integer range, the function returns a long instead.
 |
 |  If x is not a number or if base is given, then x must be a string or
 |  Unicode object representing an integer literal in the given base.  The
 |  literal can be preceded by '+' or '-' and be surrounded by whitespace.
 |  The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
 |  interpret the base from the string as an integer literal.
 |  >>> int('0b100', base=0)
 |  4

2、解释:将一个数字或者字符串转换成整型,若未传入参数时,返回0。此类用法比较多,具体见举例。

3、举例:

>>> int('10')
10
>>> int(10.98)
10
>>> int()
0
>>> int('0b100', base=0)
4
>>> int('0x100', base=0)
256

四十二、intern方法

1、定义:

intern(...)
    intern(string) -> string

    ``Intern'' the given string.  This enters the string in the (global)
    table of interned strings whose purpose is to speed up dictionary lookups.
    Return the string itself or the previously interned string object with the
    same value.

2、解释:intern参数string。 intern机制:需要值相同的字符串的时候(比如标识符),直接从池里拿来用,避免频繁的创建和销毁,提升效率,节约内存。缺点是,拼接字符串、对字符串修改之类的影响性能。

​3、举例:(略)


发表评论

* *