可能是一个小的Python包,仅包含raise_if函数
项目描述
[](https://travis-ci.org/rochacbruno/raise_if)
[](https://coveralls.io/r/rochacbruno/raise_if)


[](http://forthebadge.com)
[](http://forthebadge.com)
[](http://forthebadge.com)
# raise_if
Python可以像Ruby一样有raise条件。
```ruby
def i_must_have_truth(value)
raise TypeError, 'You must give me truth' if value == false
end
```
但只有一行选项能工作,这伤害了PEP8
```python
def i_must_have_truth(value)
if not value: raise TypeError('You must give me truth')
```
所以...
```python
$ pip install raise_if
import raise_if
def i_must_have_truth(value)
raise_if(not value, TypeError, 'You must give me truth')
```
传递异常类型和参数
```python
raise_if(not 1 == 2, TypeError, 'Fails', another_arg='foo')
```
或者
```python
raise_if(not 1 == 2, TypeError('Fails', another_arg='foo'))
```
为什么???
因为我懒,我不喜欢在if语句链中添加额外的换行符!
:)
状态](https://travis-ci.org/rochacbruno/raise_if.png)](https://travis-ci.org/rochacbruno/raise_if)
[](https://coveralls.io/r/rochacbruno/raise_if)


[](http://forthebadge.com)
[](http://forthebadge.com)
[](http://forthebadge.com)
# raise_if
Python可以像Ruby一样有raise条件。
```ruby
def i_must_have_truth(value)
raise TypeError, 'You must give me truth' if value == false
end
```
但只有一行选项能工作,这伤害了PEP8
```python
def i_must_have_truth(value)
if not value: raise TypeError('You must give me truth')
```
所以...
```python
$ pip install raise_if
import raise_if
def i_must_have_truth(value)
raise_if(not value, TypeError, 'You must give me truth')
```
传递异常类型和参数
```python
raise_if(not 1 == 2, TypeError, 'Fails', another_arg='foo')
```
或者
```python
raise_if(not 1 == 2, TypeError('Fails', another_arg='foo'))
```
为什么???
因为我懒,我不喜欢在if语句链中添加额外的换行符!
:)