【问题标题】:python variable assignment with ":"python变量赋值用“:”
【发布时间】:2020-04-25 11:29:55
【问题描述】:

def unexpected_exceptions(exctype, value, tb):
    exception = ''.join(traceback.format_exception(exctype, value, tb))
    _logger = logging.getLogger('unexpected_exceptions')
    _logger.setLevel(logging.DEBUG)

    f_handler: FileHandler = logging.FileHandler(os.path.join(BASE_PATH, 'log ' + CURRENT_VERSION + '.log'))

    f_handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s'))
    _logger.addHandler(f_handler)
    _logger.exception(exception, exc_info=True)
    f_handler.close()
    _logger.removeHandler(f_handler)
    print(value)

抱歉,我找不到任何关于此的文章

这个变量赋值是什么?

f_handler: FileHandler = logging.FileHandler(os.path.join(BASE_PATH, 'log ' + CURRENT_VERSION + '.log'))

【问题讨论】:

标签: python variables variable-assignment


【解决方案1】:

语法用于python 3.5中引入的类型提示/注释

工具使用它来提供更好的提示、静态检查等。请注意,这根本不会改变 python 的行为,只是帮助工具提供额外支持的功能。

在您提供的情况下:

t:str=1

t 被暗示为str 类型的变量。这将允许您的 IDE 在您按下 . 时在 t 上提供 str 方法!

这是来自 pydocs 类型提示的标准示例 -

下面的函数接受并返回一个字符串,并注解如下:

def greeting(name: str) -> str:
    return 'Hello ' + name

在函数greeting中,参数名称应该是str类型,返回类型是str。接受子类型作为参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2013-09-13
    • 2016-01-24
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多