From 318e60dcb7b7e6d8a5931d12f940a797882806cc Mon Sep 17 00:00:00 2001 From: yixin <2050485123@qq.com> Date: Wed, 25 Dec 2024 09:01:17 +0800 Subject: [PATCH] add comments for utils.py --- .../python/mindspore/_extends/utils.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/mindspore2022/mindspore/python/mindspore/_extends/utils.py b/src/mindspore2022/mindspore/python/mindspore/_extends/utils.py index 18f47470..ec8fdb3b 100644 --- a/src/mindspore2022/mindspore/python/mindspore/_extends/utils.py +++ b/src/mindspore2022/mindspore/python/mindspore/_extends/utils.py @@ -22,6 +22,21 @@ def cell_attr_register(fn=None, attrs=None): """ 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__ 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): + """ + 装饰器函数,用于记录类的初始化参数。 + + Args: + fn (function): 需要被装饰的函数。 + + Returns: + function: 返回一个新的函数,该函数在调用时会记录传递给fn函数的参数。 + + """ @wraps(fn) 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 = [] if attrs is None: bound_args = inspect.signature(fn).bind(self, *args, **kwargs)