Chinaunix首页 | 论坛 | 博客
  • 博客访问: 756033
  • 博文数量: 96
  • 博客积分: 2023
  • 博客等级: 上尉
  • 技术积分: 1738
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-15 10:03
文章分类

全部博文(96)

文章存档

2014年(11)

2012年(85)

分类: Python/Ruby

2014-06-17 11:40:27

  1. def buildConnectionString(params):
  2.     """Build a connection string from a dictionary of parameters.

  3.     Returns string."""
  4.     return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

  5. if __name__ == "__main__":
  6.     myParams = {"server":"mpilgrim", \
  7.                 "database":"master", \
  8.                 "uid":"sa", \
  9.                 "pwd":"secret" \
  10.                 }
  11.     print buildConnectionString(myParams)
def buildConnectionString(params):

Note that the keyword def starts the function declaration, followed by the function name, followed by the arguments in parentheses. Multiple arguments (not shown here) are separated with commas.

通过关键字def 定义了函数的开始,后面接着是函数名,函数名后面紧跟括号,括号的参数通过逗号来分隔。

Also note that the function doesn't define a return datatype. Python functions do not specify the datatype of their return value; they don't even specify whether or not they return a value. In fact, every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, the Pythonnull value.

注意,python没有定义函数的返回数据类型。python函数不必说明返回值的数据类型;甚至并不必说明函数是否返回值。事实上,每一个python都包含返回值,如果函数最后执行return语句,那么函数返回return语句值,否则函数姜返回NONE(python的空值)。
In Visual Basic, functions (that return a value) start with function, and subroutines (that do not return a value) start with sub. There are no subroutines in Python. Everything is a function, all functions return a value (even if it's None), and all functions start with def.
在VB中,带返回值的函数以function开始,不带返回值的subroutine以sub开始。在Python中没有subroutine。所有的对象(???)都是函数,所有的函数都有返回值(即使返回值为NONE,所有的函数以def开始
The argument, params, doesn't specify a datatype. In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally

函数的参数不必明确的说明数据类型。在python中,变量不必显示的声明数据类型。python解释器能够自动的判断该变量的数据类型,并在内部一直记住该数据类型。

In JavaC++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument. In Python, you never explicitly specify the datatype of anything. Based on what value you assign, Python keeps track of the datatype internally.
在Java和C++以及其它静态类型的语言中,你必需说明函数返回值的类型以及函数参数的类型。在Python中,你不必显示说明任何对象的类型。根据你的赋值,Python在内部始终记录该变量的类型。

An erudite reader sent me this explanation of how Python compares to other programming languages:

曾经有位资深读者发给我下面的内容,以便解释python与其它编程语言的比较

statically typed language  A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare all variables with their datatypes before using them. Java and C are statically typed languages.
静态类型语言:变量的类型在编译过程时就确定(由编译器确定)。大部分静态类型语言强制要求你在使用这些变量之前要声明这些变量的类型。java和C就是静态类型语言。
dynamically typed language  A language in which types are discovered at execution time; the opposite of statically typed. VBScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value.
动态类型语言:与静态类型语言相反,变量的类型在执行才(由解释器)确定的语言;VB脚本与python语言都是动态类型语言,这是因为这些语言在你第一次给变量赋值的时候才最终确定它的类型。
strongly typed language  A language in which types are always enforced. Java and Python are strongly typed. If you have an integer, you can't treat it like a string without explicitly converting it.
强类型语言:语言中变量的类型总是被强化。Java和Python同时也是强类型语言。比如你对一个整型变量,除非你显示的进行转化,否则你是不能像对待字符串那样就行某些操作。
weakly typed language  A language in which types may be ignored; the opposite of strongly typed. VBScript is weakly typed. In VBScript, you can concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123, all without any explicit conversion.
弱类型语言:与强类型语言相反,变量的类型在某些时候可以被忽略。VB脚本就是弱类型语言。在VB脚本中,不需要进行显示的转换,你可以对字符串‘12’和整数3进行拼接,然后得到整数123.
So Python is both dynamically typed (because it doesn't use explicit datatype declarations) and strongly typed (because once a variable has a datatype, it actually matters).
所以Python既是动态类型语言(因为它不需要显示的声明变量的类型),同时也是强类型语言(因为变量的类型决定了你对变量可以进行何种操作)。

阅读(1690) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~