可能是namedtuple(属于collections库的一部分)的作业。collections.namedtuple允许您通过指定的名称访问元组的不同成员。在
示例:# import the namedtuple module from the library
from collections import namedtuple as nt
# create a type of namedtuple called Time that contains days, hours, minutes
Time = nt('Time', 'days hours minutes')
# you can make a Time instance this way
t1 = Time(0, 0, 1)
# now you can print it to see what's inside
print(t1) # Time(days=0, hours=0, minutes=1)
# ...and access the parts of the Time object by name
print(t1.minutes) # 1
# ...or access them by index
print(t1[2]) # 1
现在要转换为分钟,只需执行以下操作:
^{pr2}$
或者,您也可以稍微更改一下所需的函数签名,并按这种方式进行,这样做似乎更直接一些:def minutes(t: 'time'):
return t.days*24*60 + t.hours*60 + t.minutes
#testing
t = Time(1,1,1)
assert minutes(t) == 24*60 + 60 + 1
编辑:我没有意识到问题的关键是要了解科隆在做什么。函数签名中参数后面的冒号和字符串不是字典;它们是function annotations,我相信这在Python3中是新的。在