zoukankan      html  css  js  c++  java
  • 装饰器函数 包装函数 包裹函数

    【some code will be added on the top of decorated function---Beta Words---被装饰的函数头部会被添加一些新代码且先被执行】

    https://hackernoon.com/decorators-in-python-8fd0dce93c08

    def decorator_func(say_hello_func):
        def wrapper_func(hello_var, world_var):
            hello,world = "Hello, ","World"
    
            if not hello_var:
                hello_var = hello
    
            if not world_var:
                world_var = world
    
            return say_hello_func(hello_var, world_var)
    
        return wrapper_func
    
    
    @decorator_func
    def say_hello(hello_var, world_var):
        print(hello_var + " " + world_var)
    
    
    say_hello("Hello", "")
    

      

    https://zh.wikipedia.org/wiki/包裝函式

    https://en.wikipedia.org/wiki/Wrapper_function

    计算机科学中,包装函数英语:Wrapper function)是一种计算机程序中的函数,它存在的主要目的就是用来调用另一个函数。在面向对象编程中,它又被称为方法委任(method delegation)。它的存在有多种功能:可以被当成适配器模式来使用,可以当成错误检查程序,也可以被当成多重继承来使用。

    public class StackSet implements Stack, Set {
    
        private LinkedList stack;
        private HashSet set;
    
        public boolean push(Object o) {
            if (set.add(o)) return stack.push(o);
            else return false;
        }
    
        public Object pop() {
            Object o = stack.pop();
            set.remove(o);
            return o;
        }
    
        public boolean contains(Object o) {
            return set.contains(o);
        }
    
    }

    wrapper function is a subroutine in a software library or a computer program whose main purpose is to call a second subroutine[1] or a system call with little or no additional computation. Wrapper functions are used to make writing computer programs easier by abstracting away the details of a subroutine's underlying implementation.

    rapper functions are a means of delegation and can be used for a number of purposes.

    Programming convenience

    Wrapper functions can be used to make writing computer programs easier. An example of this is the MouseAdapter and similar classes in the Java AWT library.[2] Wrapper functions are useful in the development of applications that use third-party library functions. A wrapper can be written for each of the third party functions and used in the native application. In case the third party functions change or are updated, only the wrappers in the native application need to be modified as opposed to changing all instances of third party functions in the native application.

    Adapting class/object interfaces

    Wrapper functions can be used to adapt an existing class or object to have a different interface. This is especially useful when using existing library code.

    Code testing

    Wrapper functions can be used to write error checking routines for pre-existing system functions without increasing the length of a code by a large amount by repeating the same error check for each call to the function.[3] All calls to the original function can be replaced with calls to the wrapper, allowing the programmer to forget about error checking once the wrapper is written. A test driver is a kind of wrapper function that exercises a code module, typically calling it repeatedly, with different settings or parameters, in order to rigorously pursue each possible path. It is not deliverable code, but is not throwaway code either, being typically retained for use in regression testing. An interface adaptor is a kind of wrapper function that simplifies, tailors, or amplifies the interface to a code module, with the intent of making it more intelligible or relevant to the user. It may rename parameters, combine parameters, set defaults for parameters, and the like.

    Multiple inheritance

    In a programming language that does not support multiple inheritance of base classes, wrapper functions can be used to simulate it. Below is an example of part of a Java class that "inherits" from LinkedList and HashSet. See Method for further implementation details.

    public class Test implements LinkedList, HashSet{
    
    @Override
    //contains data members and data methods
    //covariant return
    
    }
    

    Library functions and system calls

    Many library functions, such as those in the C Standard Library, act as interfaces for abstraction of system calls. The fork and execve functions in glibc are examples of this. They call the lower-level fork and execve system calls, respectively.

    This may lead to incorrectly using the terms "system call" and "syscall" to refer to higher-level library calls rather than the similarly named system calls, which they wrap.[citation needed]

    def my_decorator(f):
        from functools import wraps
        @wraps(f)
        def wrapper(*args, **kwargs):
            print('1')
            print(args)
            print(kwargs)
            return f(*args, **kwargs)
    
        return wrapper
    
    
    @my_decorator
    def ex(*args, **kwargs):
        print('2----->')
    
    
    ex(11, 22, d=33, e=44)
    
    # 需求
    # 在Django控制台打印接口调用方的请求参数
    

      

    1
    (11, 22)
    {'d': 33, 'e': 44}
    2----->
    

      

    def my_decorator(f):
        from functools import wraps
        @wraps(f)
        def wrapper(*args, **kwargs):
            print('1')
            print(args)
            print(kwargs)
            # return f(*args, **kwargs)
            return f(args, kwargs)
    
        return wrapper
    
    
    @my_decorator
    def ex(*args, **kwargs):
        print('2----->')
    
    
    ex(11, 22, d=335, e=44)
    

    1
    (11, 22)
    {'d': 335, 'e': 44}
    2----->

      

     https://en.wikipedia.org/wiki/Wrapper_function


    Library functions and system calls

    Many library functions, such as those in the C Standard Library, act as interfaces for abstraction of system calls. The fork and execve functions in glibc are examples of this. They call the lower-level fork and execve system calls, respectively.

    This may lead to incorrectly using the terms "system call" and "syscall" to refer to higher-level library calls rather than the similarly named system calls, which they wrap.[citation needed]

    装饰器 - 廖雪峰的官方网站 https://www.liaoxuefeng.com/wiki/1016959663602400/1017451662295584

    装饰器







  • 相关阅读:
    Docker 版本升级
    Docker Swarm 常用命令
    Docker Swarm 介绍 or 工作原理
    Docker OpenvSwitch 应用部署
    Docker OpenvSwitch 介绍 or 工作原理
    Solr配置与简单Demo[转]
    使用solrj操作solr索引库,solr是lucene服务器
    Apache Solr配置
    使用solr搭建你的全文检索
    [转]flume-ng+Kafka+Storm+HDFS 实时系统搭建
  • 原文地址:https://www.cnblogs.com/rsapaper/p/6965162.html
Copyright © 2011-2022 走看看