python - Why is fileinput.input object not lost when going out-of-scope? -
in following code expect python freeing fileinput.input
when i'm return
ing in middle loop going out-of-scope. however, when calling again function fileinput tells me
raise runtimeerror, "input() active"
here code:
def func(inplace): line in fileinput.input(sys.argv[1], inplace=inplace): [..] if condition: return true [..] if func(false): func(true)
i expect behavior when using yield
not when using return.
i'm using python 2.7.3.
is there way force reset of fileinput?
edit:
when calling fileinput.close()
before returning works. why not done implicitly?
edit 2: @matslindh
replacing
for line in fileinput.input(sys.argv[1], inplace=inplace):
with
for line in fileinput.fileinput(sys.argv[1], inplace=inplace):
does want, because returns object goes out-of-scope in defined manner. assumed fileinput.input()
that, no. using global instance.
there nothing here can go out of scope - you're calling function in module has been imported, , fileinput
1 of modules has global state, since future calls fileinput
maps made input
call.
you should able work around using fileinput
class instead, recreated next time call func()
, using with
object.
or - discovered, calling close()
resets internal module state.
Comments
Post a Comment