When learning the adorner chapter on the official website of Liaoche, a preliminary understanding is similar to tangent-oriented programming. Record your own after-school exercise solution.problem: write a decorator that prints the 'beginCall' and 'end calls ' before and after function invocations ' the log.Write a @log decorator that allows it to support both:@logdef f(): passalso supports:@log(‘execute‘)def f(): passSample code (personal practice, if you have questions welcome Treatise):#! usr
GetName fucntion in fluent (). So that's the decorator comes in.To use decorator to descorate a function in class:functiondecorate (target, keys, descriptor) {varfn =Descriptor.value; //Overwrite the value, which in this case is functionDescriptor.value =function(... args) {fn.apply (target, args); returnTarget; }}class person {@decorate setName (firstName, lastName) { This. FirstName =FirstName; This. LastName =LastName; } @decorate GetName () {Console.log ( This. FirstName, This. LastName);
function, in the test.py inside, is the F1 function passed to the Func, you can also inner function with the F1 function to draw an equal sign.During execution, the output before is displayed, then the value of the parameter ' Canshu ' passed in func_dy.py is output, then after, and finally, the return method of the F1 function is called, returning Kankan, where the call is more complex, It is difficult to make the detailed call process very clear, later if there are more instances of this rela
- os.path.isabs (path) returns True if path is an absolute path - os.path.isfile (path) returns True if path is a file that exists. Otherwise returns false - os.path.isdir (path) returns True if path is a directory that exists. Otherwise returns false - Os.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignored - os.path.getatime (path) returns the last access time of the file or directory to which path is pointing inOs.path.g
A deep understanding of Python decorators and a deep understanding of python decorations
Decorator introduction:
Decorator is an advanced Python syntax. The decorator Can process a function, method, or class. In Python, we have multiple methods to process functions and classes. For example, in the Python closure, we can see the function object as the return result of a function. Compared with other methods, the decorator has simple syntax and high cod
the process of invoking the call to use two times to get the effect we want. So we use the adorner method provided by Python to modify our code:defShowinfo (func):defWrap (x, y):Print "The function before"func (x, y)Print "The function after" returnWrap @showInfodeff (x, y):Printx+y @showInfodefF1 (x, y):Printx*y F (2,3) F1 (2, 3)This time the results are as follows: We just need to call our function directly to get the result we want.The function before5 thefunction Afterthe function before
1. Decorative Device1.1 Definition:essentially a function that decorates other functions-that is, adding additional functionality to other functions1.2 Adorner principle:1) cannot modify the source code of the decorated function;2) cannot modify the calling mode of the decorated function;1.3 Implement the Adorner knowledge Reserve:The 1.3.1 function is the "variable"Defining a function is equivalent to assigning a function body to a functional name.1.3.2 Higher order functionsSee the previous st
1, ConceptAdorner English name decorator, the function of the adorner is to add additional functionality to an existing object. In general, the adorner is actually a function, a function to wrap the function, so his parameters are modified function object, return a modified function object, the original identifier is re-assigned, and permanently lose access to the original function object.2. Decorator syntax1.def Deco (func): 2. print func 3. return func 4[email protected] 5. def f
= func(*args, **kwargs) print(" after %s called. result: %s" % (func.__name__, ret)) return ret return _deco@decodef myfunc(a, b): print(" myfunc(%s,%s) called." % (a, b)) return a+bHere the Decorator Deco, will wrap the MyFunc method, realize the call before and after the log information interception.How do we achieve this in coffeescript? There is no real decorator feature in Coffeescript, but it has advanced functions that can be wrapped as follows:log(myfunc)In Coffe
parameter values Was passed Here. def Wrapper (request,kargs): # The following Code's judgment logic is not important, it is important that the reference and return value of the parameter Before_result = Auth (request , Kargs) if (before_result! = None): return before_result; Main_result = Main_func (request,kargs) If (main_result! = None): return main_result; After_result = Log (request,kargs) If (after_result! = None): return after_result; Return wrapper return ou
Connection Library directory (generally in/usr/local/lib/python2. x/)4), the contents of any. pth file (if any). New feature that allows users to add an effective directory to the module search pathimporterror:no module named MyModule, this error is said: The module is not in the search path, resulting in a path search failed!Workaround: 1, add sys.path.append ("module file directory") in the calling file; 2. Using PTH files, create the. pth file in the Site-packages file, write the pat
, define a Animal class is a fuss, and the voice function after execution, its scope has been released, but the Animal class and its instance dog The corresponding properties of the property are stored in memory:This occupancy is not necessary for this function to be implemented.In addition, closures have many other functions, such as for encapsulation, etc., in addition, the closure effectively reduces the number of function parameters, which is very valuable for parallel computing, such as the
One, closed package1. Closures are defining functions inside functions and returning intrinsic functions2. Encapsulation and reuse of closures for code3. example, the internal definition of a comparison function, given the boundary value is to make different judgments、Second, the decoration device1. The adorner is the principle of using closures[Email protected] is the decorator's syntax sugar3. Adorners can add functions to functions4. Example, the function of adding parameter preprocessing to
Definition: An adorner is essentially a function that is used to decorate other functions (that is, adding additional functionality to other functions):
Cannot modify the source code of the decorated function
Cannot modify the calling method of the decorated function
Decorator Preparation Knowledge:
Function name is "variable"
Higher order functions
Pass a function name as an argument to another function (add a function to it without modifying the source
I. Adorner pre-placement knowledge essentials1. Namespacesplaces where names and variables are boundClassification of namespaces:Built-in namespaces: where the Python interpreter starts, storing its own name. (such as built-in functions)Global namespaces: Where file-level names are stored when the file is executedLocal namespaces: During the execution of a file, if the function is called, where the function is used to hold the default name, the call takes effect, and the call ends after it expir
. Remember to add @functools.wraps(func) it before the definition wrapper() .PracticePlease write a decorator that will print the log of ' Begin call ' and ' end calls ' before and after the function invocation.Think again about whether you can write a @log decorator that supports both:@logImportFunctoolsdeflog (func): @functools. Wraps (func)defWrapper (*args, * *kw):Print('%s before%s ():'% (call, Func.)__name__) ) Result= Func (*args, * *kw)Print('%s after%s ():'% (call, Func.)__name__))
, the program from the top down, go to the definition of the timer function will jump to @timer place, detected that the timer is used as an adorner, the system will go to search where to use the timer as the adorner, after the completion of the search, the execution of the timer, and then execute the test1 we call (), because test1 is decorated by a timer, so the adorner is executed first, and when it executes to func () inside the adorner, then Func () is test1 ()! Test2 () is the sameThe ef
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.