python 函数相关

[ 装饰器:]

#!/usr/bin/env/ python

from time import ctime

 

def tsfunc(func):

~ def wrappedFunc():

~ ~ print '[%s] %s() called' % ( ctime(), func.__name__)

~ ~ return func()

~ return wrappedFunc

 

@tsfunc

def foo():

~ pass

 

foo()     #=> [Sun Mar 19 22:50:28 2006] foo() called

 

[ lambda: ]

def usuallyAdd(x,y=2) : return x + y

<=> lambda x,y=2 : x+y

 

[ global: ]

def foo():

~ bar = 200

~ print bar

bar = 100

print bar     #=> 100

foo()           #=> 200

print bar     #=> 100

 

def foo2():

~ global bar

~ bar = 300

~ print bar

print bar    #=> 100

foo2()        #=> 300

print bar    #=> 300

posted on 2011-03-18 15:27  真阿当  阅读(104)  评论(0编辑  收藏  举报