学习Python日记 ------(二)

1.Python的函数的默认值

    在Python中定义默认值就像这样:

def foo(x=10)
   
print x

    另外,也可以使用变量来作为参数的默认值,如:
a=10
def foo(x=a)
   
print a   
a
=12

print foo()  #It will print 10
    对于上面来说,默认函数参数的值在被赋值时就指定,后面的a=12,改变不了x的值。但是,有一种情况会发现改变,那就是用可变对象作为参数值,就为有意外情况发生:
 a = [10]
 
def foo(x = a):
     
print x
 a.append(
20)
 foo()              
# Prints '[10, 20]'
posted @ 2006-04-03 21:36  shipfi  阅读(273)  评论(0编辑  收藏  举报