zoukankan      html  css  js  c++  java
  • Python Decorators

    It is a way to run extra processing steps at function and class definition time with explicit syntax. It comes in two flavors:

    • Function decorators—the initial entry in this set. They specify special operation modes for both simple functions and clesses’s methods by wrapping them in an extra layer of logic implemented as another function, usually called metafunction.
    • Class decorators—a later extension. They do the same for classes, adding support for management of whole objects and their interfaces. Though perhaps simpler, they often overlap in roles with metaclasses.

    A First Look at User-Defined Function Decorators

    >>> class Tracer(object):
    	def __init__(self, func):
    		self.counter = 0
    		self.func = func
    	def __call__(self, *args,**kwargs):
    		self.counter += 1
    		print("call func:%s times:%d" % (self.func.__name__, self.counter))
    		return self.func(*args, **kwargs)
    
    	
    >>> @Tracer
    def add(x, y):
    	return x + y
    
    >>> add(1, 1)
    call func:add times:1
    2
    >>> add(2, 2)
    call func:add times:2
    4
    >>> 
  • 相关阅读:
    C++ 扩展 Op
    Python 扩展 Op
    VS Code 调试 OneFlow
    运行时数据获取
    OFRecord 图片文件制数据集
    OFRecord 数据集加载
    OFRecord 数据格式
    OneFlow 并行特色
    Consistent 与 Mirrored 视角
    作业函数的定义与调用
  • 原文地址:https://www.cnblogs.com/hotbaby/p/4917596.html
Copyright © 2011-2022 走看看