Search code examples
pythonreddit

Python, does not stop at OR? [also Reddit related]


I have a question about Python, I'm not really sure if other programming languages do the same but let me explain the issue:

#Let's pretend x could be 'None' or some string.
x = None

if x is not None or len(x) != 0:
  pass
#next elif

What I'm trying to achieve is that. Should x be None in this case, then the first statement will return false and go to the "next elif". But it can't do that because I receive this error: TypeError: object of type 'NoneType' has no len()

My biggest issue is that I can't use, if x: because apperantly depending if old or new reddit, it has a different value and one of them is probably a no-width space, not sure how I can print unicodes but the line is just empty when I print x.


Solution

  • The thing is, x is not None is not true, so len(x) != 0 must be evaluated in order to determine whether the entire statement is true. If it was and instead of or, it wouldn't be evaluated. What you probably need is either if x is not None and len(x) != 0 or if x is None or len(x) == 0. In both cases, the second part of the condition wouldn't evaluate if x was None.