只是学习语句especially from this article
问题是,我能给__enter__
传递一个参数吗?
我有这样的代码:
class clippy_runner:
def __enter__(self):
self.engine = ExcelConnection(filename = "clippytest\Test.xlsx")
self.db = SQLConnection(param_dict = DATASOURCES[STAGE_RELATIONAL])
self.engine.connect()
self.db.connect()
return self
我想将文件名和参数dict作为参数传递给__enter__
。有可能吗?
您可以使用contextmanager装饰器传递参数:
https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager
我发现,使用
contextmanager
可以提供参数,但不能将它们提供给__enter__
这让我感到困惑是的,您可以通过添加一点代码来获得效果。
不。你不能。你把参数传递给
__init__()
。相关问题 更多 >
编程相关推荐