Principle python decorator - decorator process

  The definition of the decorator

  Function is to increase the existing function of extra features, it is essentially a closure function.

  It features decorators:

  Function does not modify the existing source code

  Do not modify the existing function is called

  It has a function to add additional features

  Here we use a decorator function has been achieved statistical code execution time to explain some of its principles

  method one:

  import time

  def show():

  start = time.time()

  print("show run...")

  stop = time.time()

  print ( "% s total takes seconds"% (stop-start))

  def count_number():

  start = time.time()

  s = 0

  for i in range(1000000):

  s += i

  print(s)

  stop = time.time()

  print ( "% s total takes seconds"% (stop-start))

  show()

  count_number()

  Although the code the way a function can be achieved, but we will find the code is too redundant, defined two functions have to write it again timing functions, code reusability is not high, so this method is not good.

  Second way:

  import time

  def count_time(func):

  start = time.time()

  func()

  stop = time.time()

  print ( "% s total takes seconds"% (stop-start))

  def show():

  print("show run...")

  def count_number():

  s = 0

  for i in range(1000000):

  s += i

  print(s)

  count_time(show)

  count_time(count_number)

  Second way encapsulates a function of timing, although reducing the amount of code a lot, but you will find that when we call the function, the call is not the same, so this method is not good, it modifies the function of use, if the actual development, you do not need a timer function. then you also need to change, very troublesome.

  Three ways:

  def count_time(func):

  def inner():

  start = time.time()

  func()

  stop = time.time()

  print ( "% s total takes seconds"% (stop-start))

  return inner

  def show():

  print("show run...")

  def count_number():

  s = 0

  for i in range(1000000):

  s += i

  print(s)

  # This sentence is a decorator's principle

  show = count_time(show)

  # Count_time (show) look at this is a function call,

  # We show that is passed count_time show = func

  # So count_time called in the function func () is equivalent to calling

  # Show (), so the only closure to help us complete the timing and completion of the show

  # Call the function, following the same principle count_number

  count_number = count_time(count_number)

  show()

  count_number()

  show = count_time(show)

  count_number = count_time(count_number)

  The above code is the principle decorator. We must be able to understand that this is a reference to the show function in memory among the count_time

  Three ways shorthand: Zhengzhou Women's Hospital http://www.sptdfk.com/

  If there are multiple functions need to add a login authentication function, you need to write every show = count_time (show) this function code has been decorated, this practice is quite troublesome.

  Python provides a decorative function to simpler wording, that is syntactic sugar, sugar grammar writing format is: @ decorator name, by the way syntactic sugar can also be done on the existing decorative function

  def count_time(func):

  def inner():

  start = time.time()

  func()

  stop = time.time()

  print ( "% s total takes seconds"% (stop-start))

  return inner

  # Syntax sugar ways to decorate function

  @count_time # 等价于 show = count_time(show)

  def show():

  print("show run...")

  @count_time

  # 等价于 count_number = count_time(count_number)

  def count_number():

  s = 0

  for i in range(1000000):

  s += i

  print(s)

  show()

  count_number()

  Decorator execution time is executed immediately when the load module

  We need to pay attention to:

  The closure function and only one parameter must be a function type, a function is thus defined decorator.

  Write code to follow the Open Closed Principle, which provides the function code has been implemented can not be modified, but can be extended.

Guess you like

Origin blog.51cto.com/14503791/2459955