deprecated_attribute#

astropy.utils.decorators.deprecated_attribute(name, since, message=None, alternative=None, pending=False, warning_type=<class 'astropy.utils.exceptions.AstropyDeprecationWarning'>, pending_warning_type=<class 'astropy.utils.exceptions.AstropyPendingDeprecationWarning'>)[源代码]#

用于将公共属性标记为不推荐使用。这将创建一个在访问给定属性名称时发出警告的属性。要防止警告(即内部代码),请使用属性的专用名称,方法是加一个下划线(即 self._name ),或显式设置替代项。

参数:
name : strPython :字符串

不推荐使用的属性的名称。

since : strPython :字符串

此API被弃用的版本。这是必需的。

message : str ,可选Python:字符串,可选

重写默认的弃用消息。格式说明符 name 可用于属性的名称,并且 alternative 可在否决消息中使用,以插入已弃用函数的替代名称。

alternative : str ,可选Python:字符串,可选

用户可以使用的替代属性来代替不推荐使用的属性。如果提供了此替代方案,则不推荐警告将告诉用户。

pending : bool ,可选可选的布尔

如果为True,则使用astropypendingdprecationwarning而不是 warning_type .

warning_type : Warning警告

发出警告。默认为 AstropyDeprecationWarning .

pending_warning_type : Warning警告

等待发布警告。只有在以下情况下才有效 pending 设置为True。默认值为 AstropyPendingDeprecationWarning .

实例

class MyClass:
    # Mark the old_name as deprecated
    old_name = deprecated_attribute("old_name", "0.1")

    def method(self):
        self._old_name = 42

class MyClass2:
    old_name = deprecated_attribute(
        "old_name", "1.2", alternative="new_name"
    )

    def method(self):
        self.new_name = 24