例如,我有一个对象x可能是None或float的字符串表示.我想做以下事情:
@H_301_2@do_stuff_with(float(x) if x else None)
除了不必输入x两次,就像Ruby的andand库一样:
@H_301_2@require 'andand' do_stuff_with(x.andand.to_f)解决方法
我们没有其中一个但是你自己也不难:
@H_301_2@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