- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关注 this related question ,虽然总是有一些库以独特的方式使用语言特性的例子,但我想知道是否返回 self
以外的值在 __enter__
方法应该被认为是一种反模式。
这在我看来是个坏主意的主要原因是它使包装上下文管理器成为问题。例如,在 Java 中(也可能在 C# 中),可以包装一个 AutoCloseable
另一个类中的类,它将在内部类之后进行清理,例如以下代码片段:
try (BufferedReader reader =
new BufferedReader(new FileReader("src/main/resources/input.txt"))) {
return readAllLines(reader);
}
BufferedReader
包裹
FileReader
, 并调用
FileReader
的
close()
自己的方法
close()
方法。但是,如果这是 Python,并且
FileReader
会在其
__enter__
中返回除 self 以外的对象方法,这将使这样的安排更加复杂。
BufferedReader
的作者必须解决以下问题:
FileReader
对于我自己的方法,我是否使用 FileReader
直接或其__enter__
返回的对象方法?返回的对象甚至支持哪些方法? __exit__
方法,我是否只需要关闭 FileReader
对象,或 __enter__
中返回的对象方法? __enter__
会发生什么?实际上在调用时返回了一个不同的对象?我现在是否需要保留它返回的所有不同对象的集合,以防有人调用 __enter__
几次对我?当我需要使用这些对象时,我如何知道使用哪一个? with
block - 一个用于
FileReader
, 一个用于
BufferedReader
.然而,这使我们编写了更多样板代码,并且看起来明显不那么优雅。
self
以外的东西。在
__enter__
方法,应该简单地避免这种行为。关于这些问题是否有一些官方或半官方的评论?一个负责任的 Python 开发人员应该如何编写代码来解决这些问题?
最佳答案
TLDR:返回 self
以外的其他内容来自 __enter__
非常好,也不错。
introducing PEP 343和 Context Manager specification明确将此列为所需的用例。
An example of a context manager that returns a related object is the one returned by
decimal.localcontext()
. These managers set the active decimal context to a copy of the original decimal context and then return the copy. This allows changes to be made to the current decimal context in the body of thewith
statement without affecting code outside thewith
statement.
self
以外的内容的示例。来自
__enter__
.值得注意的是,
contextlib
的大部分内容匹配这个模式。
contextlib.contextmanager
生成无法返回 self
的上下文管理器,因为没有这样的东西。 contextlib.closing
包裹 thing
并返回 __enter__
. contextlib.nullcontext
返回一个预定义的常量 threading.Lock
返回 bool 值 decimal.localcontext
返回其参数的副本 __enter__
的返回值对协议(protocol)无关紧要 .
cm.__enter__
,它负责运行cm.__exit__
. 值得注意的是,任何代码都可以访问
cm
(上下文管理器本身);
cm.__enter__
的结果无需调用
cm.__exit__
.
ContextManager
的代码必须完全运行它。任何其他代码都不必关心它的值是否来自
ContextManager
或不。
# entering a context manager requires closing it…
def managing(cm: ContextManager):
value = cm.__enter__() # must clean up `cm` after this point
try:
yield from unmanaged(value)
except BaseException as exc:
if not cm.__exit__(type(exc), exc, exc.__traceback__):
raise
else:
cm.__exit__(None, None, None)
# …other code does not need to know where its values come from
def unmanaged(smth: Any):
yield smth
__enter__
,它必须调用它的
__exit__
也是。如果外部上下文管理器已经有进入的内部上下文管理器,它不负责清理。
self
实际上是不好的做法。来自
__enter__
.返回
self
来自
__enter__
只有在
self
时才应该这样做预先完全初始化;如果
__enter__
运行任何初始化代码,应返回一个单独的对象。
class BadContextManager:
"""
Anti Pattern: Context manager is in inconsistent state before ``__enter__``
"""
def __init__(self, path):
self.path = path
self._file = None # BAD: initialisation not complete
def read(self, n: int):
return self._file.read(n) # fails before the context is entered!
def __enter__(self) -> 'BadContextManager':
self._file = open(self.path)
return self # BAD: self was not valid before
def __exit__(self, exc_type, exc_val, tb):
self._file.close()
class GoodContext:
def __init__(self, path):
self.path = path
self._file = None # GOOD: Inconsistent state not visible/used
def __enter__(self) -> TextIO:
if self._file is not None:
raise RuntimeError(f'{self.__class__.__name__} is not re-entrant')
self._file = open(self.path)
return self._file # GOOD: value was not accessible before
def __exit__(self, exc_type, exc_val, tb):
self._file.close()
GoodContext
返回一个不同的对象,它仍然负责清理。另一个上下文管理器包装
GoodContext
不需要关闭返回值,只需要调用
GoodContext.__exit__
.
关于python - 在 `self` 中返回 `__enter__` 以外的值是反模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60561959/
让我们写一个简单的类在我的脑海中解释: class SomeClass { var happyToUsed = 10 } 并创建一个对象 let someObject = SomeClass(
采用 self 的方法与采用 &self 甚至 &mut self 的方法有什么区别? 例如 impl SomeStruct { fn example1(self) { } fn ex
请观察以下代码(Win10上的python 3.6,PyCharm),函数thread0(self)作为线程成功启动,但是 thread1(self)似乎与thread0(self)不同已设置。 se
backbone.js 开始于: //Establish the root object, `window` (`self`) in the browser, or `global` on the s
做的事: self = self.init; return self; 在 Objective-C 中具有相同的效果: self.init() 快速? 例如,在这种情况下: else if([form
我查看了关于堆栈溢出的一些关于使用[weak self]和[unowned self]的问题的评论。我需要确保我理解正确。 我正在使用最新的 Xcode - Xcode 13.4,最新的 macOS
我面临在以下模型类代码中的 self.init 调用或分配给 self 之前使用 self 的错误tableview单元格项目,它发生在我尝试获取表格单元格项目的文档ID之后。 应该做什么?请推荐。
晚上好。 我对在 Swift 中转义(异步)闭包有疑问,我想知道哪种方法是解决它的最佳方法。 有一个示例函数。 func exampleFunction() { functionWithEsca
我需要在内心深处保持坚强的自我。 我知道声明[weak self]就够了外封闭仅一次。 但是guard let self = self else { return }呢? ,是否也足以为外部闭包声明一
代码 use std::{ fs::self, io::self, }; fn rmdir(path: impl AsRef) -> io::Result { fs::remo
我检查了共享相同主题的问题,但没有一个解决我遇到的这种奇怪行为: 说我有一个简单的老学校struct : struct Person { var name: String var age:
我应该解释为什么我的问题不是重复的:TypeError: can only concatenate list (not “str”) to list ...所以它不是重复的,因为该帖子处理代码中出现的
我有一个 trait,它接受一个类型参数,我想说实现这个 trait 的对象也会符合这个类型参数(使用泛型,为了 Java 的兼容性) 以下代码: trait HandleOwner[SELF
这个问题在这里已经有了答案: Why would a JavaScript variable start with a dollar sign? [duplicate] (16 个答案) 关闭 8
我总是找到一些类似的代码newPromise.promiseDispatch.apply(newPromise, message),我不明白为什么不使用newPromise.promiseDispat
我看到类似的模式 def __init__(self, x, y, z): ... self.x = x self.y = y self.z = z ... 非
mysql查询示例: SELECT a1.* FROM agreement a1 LEFT JOIN agreement a2 on a1.agreementType = a2.agreementTy
self.delegate = self; 这样做有什么问题吗?正确的做法是什么? 谢谢,尼尔。 代码: (UITextField*)initWith:(id)sender:(float)X:(flo
为什么要声明self在类中需要的结构中不需要?我不知道是否还有其他例子说明了这种情况,但在转义闭包的情况下,确实如此。如果闭包是非可选的(因此是非转义的),则不需要声明 self在两者中的任何一个。
这个问题已经有答案了: What does the ampersand (&) before `self` mean in Rust? (1 个回答) 已关闭去年。 我不清楚 self 之间有什么区别
我是一名优秀的程序员,十分优秀!