decorator is to add additional functions to existing objects.1. decorator entry 1.1. How can I meet my requirements?
The definition of the decorator is very abstract. Let's look at a small example.
def foo(): print 'in foo()'foo()
This is a boring function. But suddenly there was a more boring person. We called him B Jun and said, "I want to see how long it t
a function encapsulation of the original adorner and returns an adorner. We can interpret it as a closed packet with parameters. When we invoke @use_logging (level= "warn"), Python can discover this layer of encapsulation and pass parameters to the adorner's environment.
@use_logging (level= "warn") is equivalent to @decorator
Class
Several advanced syntax concepts of Python (lambda expression closure decorator), pythonlambda
1. Anonymous FunctionsAnonymous function is a function that is not bound to any identifier. It is used in the functional programming ages field. Typical applications:1) passed as a parameter to the high-order function (higher-order function). For example, the built-in function filter/map/reduce in
Adorner BasicsThe previous quick introduction to the adorner syntax, here, we will go deep inside the adorner working mechanism, more detailed and systematic introduction of the content of the adorner, and learn to write their own new adorners more advanced syntax.=================================================================================What is an adorner?Decorations are a way of specifying management code for functions and classes. The Python
Python decorators ii:decorator Arguments
October, 2008
(This is the second part of the chapter excerpt from the Python 3 Patterns Idioms (Python3 mode and usage), click here to read the first part)
review: Non-parameter-free Decorators
In the previous article, I described how to use decorators without parameters and use a class to implement it. Because I fin
Decorator is an advanced Python syntax. The decorator Can process a function, method, or class. This article describes how to get started with Python decorators (nine steps). If you are interested in python decorators, learn
Function summary in python: decorator closure details, python details
1. Preface
A function is also an object that can add attributes and use periods to represent attributes.
If the definition of an internal function contains a reference to an object defined in an external function (an external object can be outside an external function), an internal function is
functionality to an already existing object.adorners in PythonIn Python, the adorner implementation is very convenient.The reason: functions can be thrown away.function as an object:A. can be assigned to other variables, which can be used as the return value B. Can be defined within another functionDef:An adorner is a function, a function used to wrap a function, the adorner is called when the function declaration is completed, a modified function ob
delivery at the end:
(n,b1=1,b2=1,c=3): if n
To test this program, call Fib (1001:
>>> def Fib(n,b1=1,b2=1,c=3):... if n
If we use Fib (1002), the result is as follows:
..... File "
Now let's optimize the tail recursion.
Add a Decorator to the Fib function, as shown below:
@tail_call_optimizeddef Fib(n,b1=1,b2=1,c=3): if n
Well, this is the decorator @ tail_call_optimized. This
]# @version: A test of decorator# @date: 20121027# @desc: Just a testImportLoggingFromTimeImportTimeLogger=Logging.GetLogger()Logger.SetLevel(Logging.DEBUG)Is_debug=TrueDefCount_time(Is_debug):DefHandle_func(Func):DefHandle_args(*Args,**Kwargs):IfIs_debug:Begin=Time()Func(*Args,**Kwargs)Logging.Debug("["+Func.__name__+"],"+Str(Time()-Begin))Else:Func(*Args,**Kwargs)ReturnHandle_argsReturnHandle_funcDefpr():ForIInchRange(1,1000000):I=I*2print "Hello W
------------ Decorator -----------------------------------------------------What is an adorner?adorners are a way of specifying management code for functions and classes. The adorner itself is the form of a callable object (such as a function) that handles other callable objects. As we have seen earlier in this book, the Python adorner is presented in two related ways:
function Decorators perform n
Python Decorator Experience
Objective
Use
Add a new feature to a method
Add or delete a method to a class
Parametric decorator
Change the default invocation behavior of a method
Integration of 2 and 3
In fact, 1 and 4 can be categorized as a
parameters. When we use @use_logging(level="warn") the call, Python can discover this layer of encapsulation and pass parameters to the adorner's environment.# @use_logging (level= "warn") is equivalent to @decoratorClass DecoratorYes, the adorner can be not only a function, but also a class, compared to the function adorner, the class decoration appliance has t
), which implements the same functionality as the example code above, but looks leaner and pythonic:[Python]>>> class C ():... @classmethod # # Python's decorator syntax guarantees automatic invocation of Classmethod (FN)... def fn (x):... print X...>>> C.Fn>>> C.Fn () __main__. CTypical use cases for Classmethod:1) directly use the
Python uses the decorator for django Development Instance code, pythondjango
This article focuses on the development of django using the decorator in Python. The details are as follows.
The decorator Can process a function, method, or cl
A DefinedAn adorner is a function that adds extra functionality to an object, essentially a function. Its basic structure: higher order function + function nesting + closure. Basic knowledge explained: http://blog.51cto.com/10836356/2095118Two Adorners for simple classesLet's look at the simple class decorator, if we need to add a print function to any class, i.e
Python full stack development-Day10 decorator (closed function application), python-day10I. decorator
The decorator is an Application Scenario of closure functions.
What is a closure function? Let's recall:
Closure functions:
A function defined within a function. The functio
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply: this is a list of official Python
before or after the function is executed.
a_stand_alone_function()>>I am a stand alone function,don't you dare modify mea_stand_alone_function_decorated=my_new_decorator(a_stand_alone_function)a_stand_alone_function_decorated()>>Before the function runsI am a stand alone function,don't you dare modify meAfter the function runs
2). Use decorator
>>hello
One thing to note is that the order of the decorators has changed and the results are different.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.