python参数之间用什么分隔_如何调用和访问具有冒号分隔参数的函数的参数?

可能是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中是新的。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值