python代码问题 self._best_score: Optional[float] = None

这句代码什么意思?

    self._best_score: Optional[float] = None
import datetime
import enum
import inspect
import secrets
import time
from contextlib import ContextDecorator
from typing import Any, Dict, Optional

import torch
import torch.nn as nn

from . import random as delu_random


class _ProgressStatus(enum.Enum):
    NEUTRAL = enum.auto()
    SUCCESS = enum.auto()
    FAIL = enum.auto()


class ProgressTracker:
    """Tracks the best score, helps with early stopping.

    For `~ProgressTracker`, **the greater score is the better score**.
    At any moment the tracker is in one of the following states:

    - *success*: the last update changed the best score
    - *fail*: last :code:`n > patience` updates are not better than the best score
    - *neutral*: if neither success nor fail

    .. rubric:: Tutorial

    .. testcode::

        progress = ProgressTracker(2)
        progress.update(-999999999)
        assert progress.success  # the first update always updates the best score

        progress.update(123)
        assert progress.success
        assert progress.best_score == 123

        progress.update(0)
        assert not progress.success and not progress.fail

        progress.update(123)
        assert not progress.success and not progress.fail
        progress.update(123)
        # patience is 2 and the best score is not updated for more than 2 steps
        assert progress.fail
        assert progress.best_score == 123  # fail doesn't affect the best score
        progress.update(123)
        assert progress.fail  # still no improvements

        progress.forget_bad_updates()
        assert not progress.fail and not progress.success
        assert progress.best_score == 123
        progress.update(0)
        assert not progress.fail  # just 1 bad update (the patience is 2)

        progress.reset()
        assert not progress.fail and not progress.success
        assert progress.best_score is None
    """

    def __init__(self, patience: Optional[int], min_delta: float = 0.0) -> None:
        """Initialize self.

        Args:
            patience: Allowed number of bad updates. For example, if patience is 2, then
                2 bad updates is not a fail, but 3 bad updates is a fail. If `None`,
                then the progress tracker never fails.
            min_delta: minimal improvement over current best score to count it as
                success.

        Examples:
            .. testcode::

                progress = ProgressTracker(2)
                progress = ProgressTracker(3, 0.1)
        """
        self._patience = patience
        self._min_delta = float(min_delta)
        self._best_score: Optional[float] = None
        self._status = _ProgressStatus.NEUTRAL
        self._bad_counter = 0


    @property
    def best_score(self) -> Optional[float]:
        """The best score so far.

        If the tracker is just created/reset, return `None`.
        """
        return self._best_score

self._best_score: Optional[float] = None

肢解分析:
self 是一个实例属性
通过self绑定的属性是实例属性
实例属性 :实例与类名均可访问
Optional是一个可以为null的容器对象
Optional【float】翻译为:可选类型【浮点类型】
Optional,意思是说这个参数可以为空,但当它作为参数类型注解的时候,不代表这个参数可以不传递了,而是说这个参数可以传为 None

所以,通过肢解分析,可以得到的解释是为了更好的运行程序,防止得到了一个null对象,来解决空指针的异常带来的问题。就是这句话的用意。

参考链接:https://zhuanlan.zhihu.com/p/422426071

这种写法只有在3.6版之后才有,属于数据类型提示,就象强类型语言一样,在申请变量时,先声明数据类型
而python后来的版本中加入这种写法,是为了程序更清晰,易维护,但在程序运行时不强制检查这个变量是不是真的这种类型,只起提示作用【可看做是注释】
它一般的语法格式为:
变量赋值为  变量:数据类型 = 值    
如 a: str = ’aaaaa‘ 表示a是字符串类型,赋值为‘aaaaa’ ,但如果你写成 a: str=3也不会错,因为不会强制检查  
函数参数为  位置参数: 数据类型  默认参数:数据类型 = 值   函数返回值: -->返回值类型
def t(a: str, b: int = 32) -> str:
    return a * b
res = t('ab', 2)
print(res)
那么你的问题 self._best_score: Optional[float] = None  同样的意思:给实例属性_best_score赋值为None 但数据类型为 Optional[float] ,即可选的浮点数 或None
不了解Optional可以看标准库typing文档