0

Problem: The function [self._oun(upsplit[.....] in the code given below returns either a word (string) or a bool(FALSE). Now when the concatenation through + operator happens, it tries to concatenate a bool (say FALSE) with a string and hence fails with an exception (TypeError, concatenating bool with str). It works perfectly fine when a string is returned.

How can I change the code in a pythonic way to handle the above problem? I just tried putting an if statement before this code to check for the return value and then have two else statements, one if the returned value is bool and one with string (which does not look pythonic to me).

return ' '.join(upsplit[:word-1] + 
                        [self._oun(upsplit[word-1], 1) +
                        '-' + upsplit[word] + '-']) + \
                        ' '.join(upsplit[(word+1):])

EDIT:

If it returns FALSE I just want to append it with upsplit[word] and not the return value (which would be bool FALSE). Note that the function does not return TRUE. It either returns a modified string or it returns FALSE. If it returns FALSE than I just need to append the original string stored in upsplit[word].

4
  • 2
    What do you want it to do when the function call returns False? Commented Jun 12, 2013 at 8:33
  • I dont want to append FALSE otherwise I would have converted bool to str like mentioned in the other answers. I have edited the question. Commented Jun 12, 2013 at 8:49
  • woudn't it be easier if your function returned the orignal string rather than false? Commented Jun 12, 2013 at 8:59
  • 1
    The problem is not in this code, but in upsplit itself. In python, returning an error code is an antipattern, the function should either return something meaningful or throw an exception. Commented Jun 12, 2013 at 9:13

2 Answers 2

2
return ' '.join(upsplit[:word-1] + 
                        [self._oun(upsplit[word-1], 1) or upsplit[word] +
                        '-' + upsplit[word] + '-']) + \
                        ' '.join(upsplit[(word+1):])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for helping me on this.Actually please see my edit. I dont want to concatenate FALSE. If it returns false I want to concatenate the original string. Please see my edit.
the only thing is if the function returns an empty string or None, which are both evaluated as False, but can be considered valid in some situations.
1

wrap the call to self._oun in a str() call. str on a bool makes a string representation ('True' or 'False'), on a string, it returns the argument unchanged.

return ' '.join(upsplit[:word-1] + 
                    [str(self._oun(upsplit[word-1], 1)) +
                    '-' + upsplit[word] + '-']) + \
                    ' '.join(upsplit[(word+1):])

edit

After clarifying comment, the ternary operator in Python works as such:

a = ('a' if <test> else 'b')

in your case

(self._oun(upsplit[word-1], 1) if (self._oun(upsplit[word-1], 1) != False) else upsplit[word-1])

note that it requires 2 calls to _oun, and it would be much simpler if _oun returned the orignal string rather than False.

1 Comment

Thanks njzk2. I just put in an if/else block and two return statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.