是否存在像Ruby和?的Python库(或模式)?
发布时间:2020-12-16 21:26:56 所属栏目:百科 来源:网络整理
导读:例如,我有一个对象x可能是None或float的字符串表示.我想做以下事情: do_stuff_with(float(x) if x else None) 除了不必输入x两次,就像Ruby的andand库一样: require 'andand'do_stuff_with(x.andand.to_f) 解决方法 我们没有其中一个但是你自己也不难: def
例如,我有一个对象x可能是None或float的字符串表示.我想做以下事情:
do_stuff_with(float(x) if x else None) 除了不必输入x两次,就像Ruby的andand库一样: require 'andand' do_stuff_with(x.andand.to_f) 解决方法
我们没有其中一个但是你自己也不难:
def andand(x,func): return func(x) if x else None >>> x = '10.25' >>> andand(x,float) 10.25 >>> x = None >>> andand(x,float) is None True (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |