What happens after return statement?
Andreas Jung
lists at andreas-jung.com
Sun Oct 20 23:53:29 EDT 2002
--On Montag, 21. Oktober 2002 03:41 +0000 Gustaf Liljegren
<gustafl at algonet.se> wrote:
> I really ought to know better after 2 years with Python, but I became
> uncertain. Have a look, please:
>
># Return the contents of a file (strip DOCTYPE conditionally)
> def read_file(file, remove):
> if sys.path.exists(file):
> f = open(file, 'r')
> if remove = 0: # Just read the file
> return f.read()
> else: # Read the file and remove any line starting with '<!DOCTYPE'
> sum = ""
> while 1:
> line = f.readline()
> if line = "": break
> if line[:9] != '<!DOCTYPE':
> sum = sum + line
> return sum
> f.close() # Is the file closed (in both cases) here?
^^^^^^^^^^^^^^^^^^^
Why should that work? Tell me a comparable language that has
such a behaviour? 'return' returns the return value to where
the function was called. But there is one exception:
look at the document for try...except...finally.
'finally' allows you to specify actions that are executed in
case of an exception.
-aj
More information about the Python-list
mailing list