: Pack all parameters in the key = value form into a dict.
Function decorators with Parameters
If we need to know some additional information about the function, such as the function author, we can add parameters to the decoration function.
import datetimedef calc_spend_time(author): def first_deco(func): def new_func(*args, **kargs): start_time = datetime.datetime.now() result = func(*args, **kargs) end_tiem = datetime.datetime.now() print author, "result:", result, "used:", (end_tiem
The recently seen Liaoche Python tutorial is a great boon for anyone learning Python, with no lengthy nonsense, only the most popular short words, and the clearest examples.Here is my summary of this section of the adorner, as well as my own understanding.Note: "The code in this article refers to the tutorial above"Many times I would like to combine a lot of Python
Python are not the same as those in Java and C ++, functions in Python can be passed to another function as parameters like common variables. For example:
def foo(): print("foo")def bar(func): func()bar(foo)
Officially return to our topic. The decorator is essentially a Python function or
decorator.
Timeout detection logic: Start a new sub-thread to execute the specified method. The main thread waits for the running result of the sub-thread. If the sub-thread has not completed execution within the specified time, it is deemed as timeout, throw a timeout exception and kill the sub-thread. Otherwise, the return value of the method executed by the sub-thread is returned.
In the implementation process, it is found that there is no way to
Decorator is an advanced Python syntax. The decorator Can process a function, method, or class. Next, this article will introduce you to the basics of python decorator. If you are interested in the knowledge of
Python deep learning decorator and python deep learning and Decoration
Decorator is an advanced Python syntax. The decorator Can process a function, method, or class. In
Decorator in Python and Python decorator
Decorator in Python
Assume that the following requirements exist:You have defined a function. You do not want to modify the code of the function when you want to dynamically add it at runti
set of functions and called.
Higher order function : one function accepts another function as a variable. function is variable
all objects in Python (objects, and then the problem with object programming). When a function is defined, the function is the first class object/class object. The so-called first class
ArticleDirectory
1. Origin
2. Make the code more elegant.
3. Make the code more elegant.
4. Add Parameters
5. Decorated functions with return values
6. apply multiple decorators
7. Flexible Use
To understand Python's decorator, you must first know that the function is also an object in Python, so you can
Copy a function to a variable
Use a function as a parameter
, it provides us with a warm effect.
Before talking about the decorator, you must first understand one thing. the functions in Python are not the same as those in Java and C ++, functions in Python can be passed to another function as parameters like common variables. for example:
def foo(): print("foo")def bar(func): func()bar(foo)
Officially return to our
Decorator and python decorator
1. What is a decoration device?
The decorator is essentially a function. Its function is to decorate other functions and add additional functions to other functions.
2. Application scenarios
If our program has been launched, we want to add new features for it. We originally wanted to modi
First, the adorner decorator
The decorator design pattern allows you to dynamically wrap existing objects or functions so that you can modify existing responsibilities and behaviors, simply to dynamically extend existing functionality. In fact, the concept of AOP in other languages separates the real functions of objects or functions from other auxiliary functions.
Second, the
;Now () - Execute Now: the2017-12-16Compared to the two-layer nested decorator, the 3-layer nesting effect is this:1 >>> now = log ('execute') (now)Third, Functools.wrapsThere is no problem with the definitions of the two decorator, but the last step is not the case. Because we're talking about functions and objects, and it has __name__ properties like that, but you see the functions after the
Python tutorial 11 -- Python decorator
A function is also an object, and a function object can be assigned to a variable. Therefore, a function can also be called through a variable.
The function object has a (Double underline + name + double underline) attribute. You can get the function name:
Now, let's assume that we want to enhance the functions of the now (
Python's decorator is similar to java/c# 's annotation in its use, which is to decorate something for this method by adding a @xxx annotation to the method name. However, Java/c# 's annotation is also very daunting, too TMD complex, you want to play it, you need to know a bunch of annotation library documents, let people feel is learning another language. Python uses a very elegant method relative to
summary, the purpose of the decorator is to add additional functions to existing objects.
1.1. How does the demand come from?
The definition of the decorator is very abstract. Let's look at a small example.Copy codeThe Code is as follows: 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 took to
The Python decorator, closure, and functools tutorials are described in detail.
Decorators)
The decorator is a design pattern. If a class wants to add some functions of other classes without inheriting or directly modifying the source code, you can use the decorator pattern.
Python's adorner use is a very important part of the Python language, which is the embodiment of the decorative pattern in the programming pattern, and Python provides a special syntactic sugar that can be easily implemented in decorative mode.Series Articles
A single-instance pattern of Python design patterns (i)
Summary of common creation patterns of
, annotations are added to both Java and C. Of course, they also have some problems: sometimes you have to bypass many pitfalls to achieve your goals. These annotation features are also tied to some inherent features of these languages (like the "Directing" described by Martin Fowler ")A little different is that many C ++ programmers, including me, have realized the power of C ++ templates and are using this function like using macros.Many other languages also contain macro functions. although I
Python decorator if used well, it is the great God, use the best not to use ...The adorner (decorator) mainly contains two large properties:1, can not change the original function call mode2, can not change the original function of the codeThe first one says I don't need to change the structure of the original code.The second one says, I don't need to change the
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.