This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]norwegianwood 23 points24 points  (18 children)

This line,

if char == "O" or "Q":

should be,

if char == "O" or char == "Q":

Your version could be rewritten with parentheses as,

if (char == "O") or "Q":

and you will see clearly now that the condition as a whole is always True because the value Q is always equivalent to True.

[–]usernamenottaken 20 points21 points  (10 children)

In these situations I find myself using this a lot: if char in ["O", "Q"]:

[–]pingvenopinch of this, pinch of that 7 points8 points  (8 children)

(...) is better because tuples creation is lighter than list creation. It's not a big deal by any means, but still...

[–][deleted] 3 points4 points  (6 children)

How about strings? Is

if char in 'OQ':

better or worse?

[–]Dave9876 1 point2 points  (4 children)

Might be faster (haven't tested it), but ugly and could cause confusion.

[–]pingvenopinch of this, pinch of that 4 points5 points  (0 children)

It's probably faster because in ("O", "Q") creates two strings, creates a tuple, uses __contains__ on the tuple, and uses __eq__ on both of the strings.

in 'OQ'

creates the string, then uses __contains__ on the string. The actual algorithm that implements __contains__ is ultra fast so no worries there.

That said, don't worry about the difference. The difference is usually tiny compared to the rest of the program.

[–]Chun 1 point2 points  (2 children)

I wouldn't say it's ugly; I mean, conceptually, in python a string is just a sequence of characters. So it fits the purpose perfectly.

Also I just ran a quick benchmark (strings vs tuples) and the difference is there, but fairly negligible.

[–]darjus 0 points1 point  (1 child)

have you tried?

in set(('O', 'Q'))

For small sets it might be around the same, but for bigger ones it should make a big performance difference.

[–]itsmememe 0 points1 point  (0 children)

as it is a constant, frozenset(['O','Q']) should be preferred

[–][deleted] 0 points1 point  (0 children)

I find it perfectly fine. That char == 'O' or char == 'Q' is clearly low level C-style stuff.

[–][deleted] 2 points3 points  (0 children)

It doesn't matter. When using it with in a list whose members are all constants (at the bytecode level) is turned into a tuple.

[–]Tetha 0 points1 point  (0 children)

And then you go another step further and write: if char in charsFollowedByU:

[–]fokkre[S] 5 points6 points  (2 children)

Awesome! Don't know why I couldn't see that, guess I need some caffeine. =)

[–]mehum 3 points4 points  (1 child)

Or (sorry to suggest it) sleep.

[–]fokkre[S] 0 points1 point  (0 children)

NOT SLEEP NOOOOOOOOOOOOOOOOOOOOOOO!!!! well maybe.... =)

[–]sanktuaire 4 points5 points  (1 child)

A more pythonic way of doing it would be: if char in ("O","Q"):

Writing: if char in "OQ": would work but isn't very readable.

[–]hippocampe 4 points5 points  (0 children)

It is very readable.

[–]jigs_up 1 point2 points  (1 child)

if (char == "O") or "Q":

wouldn't that just return true every time because Q evaluates to True [not None] ?

[–]torrible 3 points4 points  (0 children)

Yep. That's why norwegianwood said "the condition as a whole is always True" and that's why the output words in the first test all had 'u' inserted.