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

  • 2016-03-01 23:47:46
  • 1113
  • 0

十八、copyright方法、credits方法

​此二种方法不做多解释, 直接上例子:

>>> credits
    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information.
>>> copyright
Copyright (c) 2001-2014 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.

十九、delattr方法

​1、定义:

delattr(...)
    delattr(object, name)

    Delete a named attribute on an object; delattr(x, 'y') is equivalent to
    ``del x.y''.

2、解释:删除一个对象已定义的属性,相当于 del object.name

(以下省略)

阅读更多

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

  • 2016-02-28 00:13:24
  • 1226
  • 0

十六、complie方法

1、定义:

compile(...)
    compile(source, filename, mode[, flags[, dont_inherit]]) -> code object

    Compile the source string (a Python module, statement or expression)
    into a code object that can be executed by the exec statement or eval().
    The filename will be used for run-time error messages.
    The mode must be 'exec' to compile a module, 'single' to compile a
    single (interactive) statement, or 'eval' to compile an expression.
    The flags argument, if present, controls which future statements influence
    the compilation of the code.
    The dont_inherit argument, if non-zero, stops the compilation inheriting
    the effects of any future statements in effect in the code calling
    compile; if absent or zero these statements do influence the compilation,
    in addition to any features explicitly specified.

2、解释:将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。

3、举例:

例1:
>>> code = "for i in range(0, 10): print i"
>>> cmpcode = compile(code, '', 'exec')
>>> exec cmpcode
0
1
2
3
4
5
6
7
8
9
>>> str = "3 * 4 + 5"
>>> a = compile(str,'','eval')
>>> eval(a)
17
例2:
test.cfg内容如下:
SQLALCHEMY_POOL_RECYCLE = 10
SQLALCHEMY_ECHO = False
filename = 'test.cfg'
_dict = {}
with open(filename) as config_file:
    code_obj = compile(config_file.read(), '', 'exec')
exec(code_obj, _dict)
print _dict.get('SQLALCHEMY_POOL_RECYCLE')
print _dict.get('SQLALCHEMY_ECHO')

输出:
10
False
因此_dict 变量获得了test.cfg文件中的配置信息

十七、complex类

1、定义:

class complex(object)
 |  complex(real[, imag]) -> complex number
 |
 |  Create a complex number from a real part and an optional imaginary part.
 |  This is equivalent to (real + imag*1j) where imag defaults to 0.

2、解释:生成一个复数

3、举例:

>>> complex(1,5)
(1+5j)
>>> complex(1)
(1+0j)
>>> complex(1,-5)
(1-5j)

阅读更多

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

  • 2016-02-24 00:08:18
  • 1101
  • 0

十二、chr方法

1、定义:

chr(...)
    chr(i) -> character

    Return a string of one character with ordinal i; 0 <= i < 256.

2、解释:将一个整型转换成字符即将ASCII码转换成字符

3、举例:

>>> chr(97)
'a'
>>> chr(107)
'k'
>>> chr(50)
'2'

十三、classmethod类

(以下省略)

阅读更多