r/Python • • Feb 28 '13

What's the one code snippet/python trick/etc did you wish you knew when you learned python?

I think this is cool:

import this

259 Upvotes

307 comments sorted by

View all comments

1

u/matthewguitar Feb 28 '13

Also:

bool('True')

True

bool('False')

True

You have to use 1 and 0 instead :)

1

u/Mecdemort Mar 01 '13

You're taking the bool of a string, and any string besides '' is true:

>>> bool('')
False

1

u/njharman I use Python 3 Mar 01 '13

You have to use 1 and 0 instead :)

Great gawds No!

Use True and False. The values, not strings 'True' and 'False'.

1

u/matthewguitar Mar 01 '13

No, I'm talking about serializing something into a bool.

For example, if I receive a string and want to interpret it as a bool, I can't use bool('True') since all strings are True. I have to receive something like 1 or 0 then go bool(int('0'))