使用函数注释时未定义的名称

时间:2015-02-04 01:43:38

标签: python function python-3.x annotations

我试图用hh:mm来表达时间。我知道冒号告诉python从几小时到几分钟,但这不是我想要的。有人能解释一种纠正我的错误的方法吗?

def extract_hours(hours:minutes):
    """returns an integer representing the number of hours number, number -> number"""
    return (hours)

这是我得到的错误:

NameError: name 'minutes' is not defined

1 个答案:

答案 0 :(得分:0)

Function annotations需要有效expressions。这意味着,您不能在注释中使用未定义的名称,否则您将引发NameError

相反,您可以使用字符串文字:

def extract_hours(hours:'minutes'):

这是有效的,因为字符串文字是表达式。