It sounds as if you want something like..
if result = some_function(arg1, arg2):
return result
This is very deliberately not possible in Python. It's too common a typo to write if a = b
instead of if a == b
, and allowing this mixes assignment with flow-control. If this is necessary, split it into two lines:
x = some_function()
if x:
print "Function returned True"
A more practical example of this is..
result = re.match("a", "b")
if result:
print result.groups()
(more correctly, you should do if result is not None:
in this case, although the above works)
In your specific case ("to verify that my internal functions return successfully"), it sounds like you should use exceptions. If everything is fine, just return whatever you wish. If something goes badly, raise an exception.
Exceptions in Python aren't like many other languages - for example, they are used internally flow control (such as the StopIteration exception)
I'd consider the following far more Pythonic than using return codes:
#!/usr/bin/env python2.6
def some_function(arg1, arg2):
if arg1 + arg2 > 5:
return "some data for you"
else:
raise ValueError("Could not complete, arg1+arg2 was too small")
Then, you can call the function in a single line:
return some_function(3, 2)
This either returns the value, or raises an exception, which you could handle the exception somewhere sensible:
def main():
try:
result = some_function(3, 5)
except ValueError, errormsg:
print errormsg
sys.exit(1)
else:
print "Everything is perfect, the result was {0}".format(result)
Or if this case is actually an error, simply let halt the application with a nice stack trace.
Yes, it's much longer than one line, but the idea behind Python is brevity, but explicitness and readability.
Basically, if the function can no longer continue, raise an exception. Handle this exception either where you can recover from the problem, or present the user with an error message.. unless you are writing a library, in which case leave the exception to run up the stack to the calling code
Or, in poem form:
$ python -m this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Finally, it might be worth reading over "PEP 8", the style guide for Python. It may answer some of your questions, such as "Are one-liner if statements 'pythonic'?"
Compound statements (multiple statements on the same line) are generally discouraged.
Yes:
if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()
Rather not:
if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()