with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
処理1
処理2
1 件の回答
これらの記事とPEP 343,346に情報があるようです。
with文の仕様がどんなものか、どのような動きをするものかが説明されているのでしょう。
Is Python with statement exactly equivalent to a try - (except) - finally block?
What is the equivalent try statement of the with statement?
PEP 343 – The "with" Statement
PEP 346 – User Defined ("with") Statements
こうした構文が:
with EXPR as VAR: BLOCK
こちらに展開されると書いてあります。
mgr = (EXPR) exit = type(mgr).__exit__ # Not calling it yet value = type(mgr).__enter__(mgr) exc = True try: try: VAR = value # Only if "as VAR" is present BLOCK except: # The exceptional case is handled here exc = False if not exit(mgr, *sys.exc_info()): raise # The exception is swallowed if exit() returns true finally: # The normal and non-local-goto cases are handled here if exc: exit(mgr, None, None, None)
質問のように、EXPR as VAR
の部分が2つ存在するのだとしたら、それぞれ用にmgr,exit,value
のセットを作成して、mgr1,exit1,value1
,mgr2,exit2,value2
とでもしてみれば出来そうな感じです。
完全には等価に出来ないかもしれませんが、試してみてください。
with
使わず実現できる(はずだ)けど, デメリットしかなさそうな?