0
我寫過一個既可以作爲上下文管理器又可以作爲函數的函數。什麼是用裝飾器和上下文管理器的函數來改變dir最Pythonic的方式?
我的功能工作在Python 2.6和作品對這個測試:
@cd('/')
def test_cd_decorator():
assert os.getcwd() == '/'
def test_cd():
old = os.getcwd()
with cd('/'):
assert os.getcwd() == '/'
assert os.getcwd() == old
test_cd_decorator()
assert os.getcwd() == old
test_cd()
什麼是最Python的解決方案?
你的失敗是什麼? –
@WayneWerner沒有失敗。我有一個解決方案,但想知道別人如何解決它。 – guettli
我編輯了你的問題,以清楚表明你只是尋找最Pythonic的解決方案。 –