Example 2.1 odbchelper.py
def
buildConnectionString(params):
"""
Build a connection string from a dictionary of parameters.
Return string.
"""
return
"
;
"
.join([
"
%s=%s
"
%
(k, v)
for
k, v
in
params.items()])
if
__name__
==
"
__main__
"
:
myParams
=
{
"
server
"
:
"
mpilgrim
"
, \
"
database
"
:
"
master
"
, \
"
uid
"
:
"
sa
"
, \
"
pwd
"
:
"
secret
"
}
print
buildConnectionString(myParams)
print
buildConnectionString.
__doc__
1、函数(function)定义无需指定返回类型。事实上,所有python函数都会返回一个值。当你的函数定义没有return语句时,函数将返回None。多个函数参数(function argument)用逗号隔开。(这里有一个疑问,为什么是function argument而不是function parameter呢?@@)
2、Python是一个动态的、
强类型的语言(dynamically & strongly type language)
3、注释函数:(documenting function)以"""开始,以"""结束,可以通过内建属性 __doc__ 获得。而且注释必须在函数最开始部分先定义,否则调用__doc__时你将得到None.
4、Everything is a object!
5、if __name__ == "__main__":
这是在测试模块。在上例中当模块独立运行时,模块的内建属性 __name__ == "__main__",当模块被import时,__name__ == "odbchelper"