add comments for utils.py

branch-yixin
yixin 7 months ago
parent 4e2d1b2b99
commit 318e60dcb7

@ -22,6 +22,21 @@ def cell_attr_register(fn=None, attrs=None):
""" """
Cell init attributes register. Cell init attributes register.
Args:
fn (function, optional): The __init__ function of the cell. Defaults to None.
attrs (list(string) | string, optional): A list of attributes to register.
Can be a list of strings or a single string. Defaults to None.
Returns:
function: The original function wrapped with attribute registration.
该函数用于注册cell类的初始化属性
通过装饰器模式将cell类的__init__函数的参数保存为operator的属性
如果未提供fn参数则返回装饰器函数wrap_cell否则返回包装后的__init__函数
"""
"""
Cell init attributes register.
Registering the decorator of the built-in operator cell __init__ Registering the decorator of the built-in operator cell __init__
function will add save all the parameters of __init__ as operator attributes. function will add save all the parameters of __init__ as operator attributes.
@ -34,8 +49,38 @@ def cell_attr_register(fn=None, attrs=None):
""" """
def wrap_cell(fn): def wrap_cell(fn):
"""
装饰器函数用于记录类的初始化参数
Args:
fn (function): 需要被装饰的函数
Returns:
function: 返回一个新的函数该函数在调用时会记录传递给fn函数的参数
"""
@wraps(fn) @wraps(fn)
def deco(self, *args, **kwargs): def deco(self, *args, **kwargs):
"""
这是一个装饰器函数用于记录类的初始化参数
Args:
self: 类实例对象
*args: 传递给被装饰函数的可变位置参数
**kwargs: 传递给被装饰函数的可变关键字参数
attrs: 可选参数指定要记录的属性可以是字符串或字符串列表
Returns:
None
Raises:
ValueError: 如果attrs不是字符串或字符串列表或者attrs中的元素不是字符串时抛出
该函数的主要作用是在类实例初始化时记录传递给__init__方法的参数
如果attrs为None则记录所有传递给__init__方法的参数不包括self
如果attrs为字符串或字符串列表则只记录指定的属性
记录的参数将被保存为实例的cell_init_args属性格式为"类名+参数列表"
"""
arguments = [] arguments = []
if attrs is None: if attrs is None:
bound_args = inspect.signature(fn).bind(self, *args, **kwargs) bound_args = inspect.signature(fn).bind(self, *args, **kwargs)

Loading…
Cancel
Save