便利套餐
nihilo的Python项目详细描述
此软件包允许您进行奇妙的导入:
>>> from nihilo import nihil
这是一个很好的对象,它的行为像没有,但接受方法调用。 你为什么要这个?
所以,我们有:
>>> a_dict = { 'who': 'Parmenides' } >>> print a_dict.get('who') Parmenides >>> print a_dict.get('where') None
- 但您可能需要这样做:
>>> a_dict.get('who').startswith('g') False >>> a_dict.get('where').startswith('g') Traceback (most recent call last): ... AttributeError: 'NoneType' object has no attribute 'startswith'
- 使用nihil,您可以做到:
>>> print a_dict.get('where', nihil).startswith('g') nihil()
注意,casting to bool返回false,因此您可以轻松测试..:
>>> if a_dict.get('where', nihil).startwith('g'): ... print "got it!"
>>> nihil.foo.bar('quux', frop=True) nihil()
记住,前尼希罗·尼希尔适合。