2005年06月07日 星期二 22:22
继续:函数
21:函数说明
注意缩进,注意冒号分隔,参数不用类型说明
函数会根据需要来调整参数的类型的。(我这样说
对吗?)
def ():
函数名称只是一个变量,可以赋给别的变量,象c中的
函数指针,另外从这里开始我要体会一下什么是面向对象。
据说Python中就把 函数当作对象来用的,所以才可以这么
方便的赋值给别的变量。
22:参数的设置
默认参数,和c++的很象
>>> def myrange(start=0,stop,step=1):
... print stop,start,step
...
这样的声明在 c++里是不可以的
>>> def printValues(name,height=170,weight=70):
... print("%s's height is %d cm,"
... " his weigth is %d kg\n"%(name,height,weight))
...
>>> printValues("Charles",weight=85)
Charles's height is 170 cm, his weigth is 85 kg
这样的调用也是在c++里面不可以的
参数个数可变
就是把后面的参数转换成tuple或 dictionary 来操作
>>> def printf(format,*arg):
... print format%arg
...
>>> printf ("%d is greater than %d",1,2)
1 is greater than 2
>>> def printf(format,**keyword):
... for k in keyword.keys():
... print "keyword[%s] is %s"%(k,keyword[k])
...
>>> printf("ok",One=1,Two=2,Three=3)
keyword[Three] is 3
keyword[Two] is 2
keyword[One] is 1
注意**dictionary 和*tuple 必须是最后一个参数
注意**对应dictionary ,*对应tuple。
在这里就要体会python的灵活了,没有很苛刻的语法的限制。
23:Doc String函数描述
可以输出函数的说明,但是注意只能输出一段,我更希望多输出几段
>>> def testfun():
... """
... this function do nothing, just demostrate the use of the
... doc string.
... """
... pass
... """
... continue:haha
... """
...
>>> print testfun.__doc__
this function do nothing, just demostrate the use of the
doc string.
>>>
同样我们可以获得其它常用函数的__doc__ 如:
>>> print [].count.__doc__
L.count(value) -> integer -- return number of occurrences of value
>>>
我应该把我写的函数都做好__doc__的工作
24:lambda函数
我不会LISP编程,我以前没见过lambda函数
资料中讲可以认为是一个简单的匿名函数,
我不知道该怎么理解。先死记硬背下来了。
>>> f= lambda e,t : e*t
>>> f(43,2)
86
>>>
>>> f("sd",2)
'sdsd'
>>>
25:函数的作用域
LGB准则就是Python找变量的次序,
先 Local name space
再 Global name space
后 Builde in name space
用global 语句可以修改作用域
注意 一定是在local name space中使用这个变量之前
>>> def fun():
... a=4
... print a
... global a
... print a
... a = 5
...
...
:1: SyntaxWarning: name 'a' is assigned to before
global declaration
>>>
26:嵌套函数
Python中函数是可以嵌套的,就是函数里面可以定义函数如:
>>> def fun(a,b):
... def aa(x,y):
... return x+y
... return aa(a,b)
...
>>> fun("pp","mm")
'ppmm'
>>>
注意里层的函数是不能访问外层的变量的
27:参数传递
python是按照 值传递 来传递参数的,但是也可以修改参数所指的值 如:
>>> def change(x,y):
... x=2
... y[0]="new"
...
>>> x=1
>>> y=["old1","old2"]
>>> change(x,y)
>>> x
1
>>> y
['new', 'old2']
>>>
这个也好理解的。y[0]的作用范围不仅仅在change的函数里面的
邹胖小 2005年6月7日 祝大家快乐安康 编程要的就是苦练勤学
2005年06月07日 星期二 23:50
Chao Zou wrote:
[snip]
> 22:参数的设置
> 默认参数,和c++的很象
> >>> def myrange(start=0,stop,step=1):
> ... print stop,start,step
> ...
带默认值的参数必须放在无默认值的参数后面,你定义的这个myrange是不合语法的。
> 这样的声明在 c++里是不可以的
C++里是可以给参数设定默认值的。
[snip]
> 26:嵌套函数
> Python中函数是可以嵌套的,就是函数里面可以定义函数如:
> >>> def fun(a,b):
> ... def aa(x,y):
> ... return x+y
> ... return aa(a,b)
> ...
> >>> fun("pp","mm")
> 'ppmm'
> >>>
> 注意里层的函数是不能访问外层的变量的
里层的函数是可以引用外层变量的
def fun(a):
def aa():
print a
return aa()
[snip]
--
Qiangning Hong
___________________________________________________________
/ critic, n.: \
| |
| A person who boasts himself hard to please because nobody |
| tries to please him. |
| |
\ -- Ambrose Bierce, "The Devil's Dictionary" /
-----------------------------------------------------------
\
\ ....
........ .
. .
. .
......... .......
..............................
Elephant inside ASCII snake
2005年06月08日 星期三 09:28
*Table 13-1.** Function argument-matching forms*
*Syntax*
*Location*
*Interpretation*
func(value)**
Caller**
Caller Normal argument: matched by position**
func(name=value)**
Caller**
Keyword argument: matched by name**
def func(name)**
Function**
Normal argument: matches any by position or name
def func(name=value)**
Function**
Default argument value, if not passed in the call**
def func(*name)**
Function**
Matches remaining positional args (in a tuple)**
def func(**name)
* *
Function **
Matches remaining keyword args (in a dictionary)**
>From "Learning Python", 2nd ed. Page 243
def F2(a1, * args, ** aw):
print "tuple", args
print "dict", aw
def F3(**keys):
print "dict", keys
F2(1, 2, 3, 4, 5, 6, a='b')
F3(a=1)
Output:
tuple (2, 3, 4, 5, 6)
dict {'a': 'b'}
{'a': 1}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050608/6b038ca3/attachment.html
2005年06月08日 星期三 22:49
>
> 22:参数的设置
> 默认参数,和c++的很象
> >>> def myrange(start=0,stop,step=1):
> ... print stop,start,step
> ...
> 这样的声明在 c++里是不可以的
>
在Python中也报错呀。
>>> def myrange(start=0,stop,step=1):
... print start, stop, step
SyntaxError: non-default argument follows default argument
>
> 23:Doc String函数描述
> 可以输出函数的说明,但是注意只能输出一段,我更希望多输出几段
>
在函数中的字符串已经是语句了。
>
> 24:lambda函数
> 我不会LISP编程,我以前没见过lambda函数
> 资料中讲可以认为是一个简单的匿名函数,
> 我不知道该怎么理解。先死记硬背下来了。
匿名函数就是它本身没有函数名,但它会返回一个函数。常用在很简单的计算中,特别是放在数据结构中,如list, dict中。
>
>
> 26:嵌套函数
> Python中函数是可以嵌套的,就是函数里面可以定义函数如:
> >>> def fun(a,b):
> ... def aa(x,y):
> ... return x+y
> ... return aa(a,b)
> ...
> >>> fun("pp","mm")
> 'ppmm'
> >>>
> 注意里层的函数是不能访问外层的变量的
不对。看例子:
>>> def a(b):
... m = 9
... def c(d):
... print b, d, m
... c('3')
>>> a(1)
1 3 9
里层C函数可以访问外层a的参数和局部变量。
>
> 27:参数传递
> python是按照 值传递 来传递参数的,但是也可以修改参数所指的值 如:
>
不太正确。应该是可以修改可变参数的值,如list, object, dict
> >>> def change(x,y):
> ... x=2
> ... y[0]="new"
> ...
> >>> x=1
> >>> y=["old1","old2"]
> >>> change(x,y)
> >>> x
> 1
> >>> y
> ['new', 'old2']
> >>>
> 这个也好理解的。y[0]的作用范围不仅仅在change的函数里面的
这种看法是因为外面的变量与参数名相同的一种误解,如果上面change(x,y)改为change(a,b),那么变成:b[0]它的作用域在哪里呢?
>
> 邹胖小 2005年6月7日 祝大家快乐安康 编程要的就是苦练勤学
>
建议多做测试,而不是光看教程。
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
--
I like python!
My Donews Blog: http://www.donews.net/limodou
New Google Maillist: http://groups-beta.google.com/group/python-cn
2005年06月09日 星期四 09:23
这个和Java常说的“参数是值传递”一样,引用本身就是一个指针值(或经过包装), 是值传递的,你的例子中,x=2已经改变了x这个引用所引用的对象,但外面的x不 受影响。y则不同,你改变的是它的值,如果你同样使用y=['new', 'old2'],则函 数外的y值依然不变。 >>27:参数传递 >> python是按照 值传递 来传递参数的,但是也可以修改参数所指的值 如: >> >> >>> def change(x,y): >>> ... x=2 >>> ... y[0]="new" >>> ... >>> >>> x=1 >>> >>> y=["old1","old2"] >>> >>> change(x,y) >>> >>> x >>> 1 >>> >>> y >>> ['new', 'old2'] >>> >>> >>> 这个也好理解的。y[0]的作用范围不仅仅在change的函数里面的 >>
2005年06月14日 星期二 12:45
python中国[pythonchina.org]组织已经成立,欢迎热爱python的兄弟姐妹来互相学习 地址:http://pythonchina.org http://pythonchina.org/bbs/ 同时欢迎有能力的同学来应聘斑竹。 python中国组织成立的目的就是为了创建一个大家互相帮助的学习社区,让大家共同进步! --------------------------------- DO YOU YAHOO!? 雅虎免费G邮箱-中国第一绝无垃圾邮件骚扰超大邮箱 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050614/fbe081fb/attachment.html
Zeuux © 2025
京ICP备05028076号