我学Python - 第二天

1.

Python里的pass语句。编写代码的时候可能要先把结构定下来,同时不干扰已经完成的代码,放一个pass。

 

2.

while-else while后面加else,好神奇的存在^_^

 

3.

在装饰器中置入通用功能的代码可以降低程序复杂度。譬如:引入日志,增加计时逻辑来检测性能,给函数增加事务的能力。

from time import ctime, sleep

 

def tsfunc(func):

  def wrappendFunc():

    print '[%s] %s() called' % (ctime(), fun._name_)

    return func()

  return wrappendFunc

 

@tsfunc

def foo():

  pass

 

foo()

sleep(4)

 

for i in range(2)

  sleep(1)

  foo()

运行脚本,得到输出:

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

[Sun Mar 19 22:50:33 2006] foo() called

[Sun Mar 19 22:50:34 2006] foo() called

 

posted on 2017-06-23 14:56  NullSir  阅读(121)  评论(0编辑  收藏  举报

导航