Won't this evaluate `string.encode('utf-8')` regardless of the value of `is_string`? The `X if COND else Y` construct doesn't do short-circuit evaluation, IIRC. The only alternative might be:

```python
encoded = string
if is_string:
    encoded = string.encode('utf-8')
```