Take the 2-minute tour ×

When I do something like (totally random example dont read into variable names):

variable = read_file() if read_file() else "File was empty"

In this case does read_file() get excuted twice? If so is there a way to do it to only execute once but keep it within one line?

share|improve this question
It's probably a bad idea to do this; how does the code that uses variable know that the file didn't actually contain "File was empty"? –  Karl Knechtel Jun 10 '12 at 10:48
Again totally random variable/method names –  NoviceCoding Jun 10 '12 at 17:47
add comment

1 Answer

up vote 19 down vote accepted

In that case read_file() would get executed twice. You can do this instead:

variable = read_file() or "File was empty"
share|improve this answer
@NoviceCoding No problem :D –  jamylak Jun 10 '12 at 9:24
add comment

Your Answer


