对于要在with块中使用的类或函数是否存在通用命名约定,例如
Is there a general naming convention for classes or functions that are meant to be used in with block such as
with CreateSomeContext() as x: ...?信号表明类或函数的结果应与with一起使用?
? Something that signals that the class or the result of a function should be used with with?
推荐答案在相应的PEP 0343 ,其中提到了两个约定:
In the respective PEP 0343, there is a mention of two conventions:
示例上下文名称中使用的时态不是 随意的.当名称指代一个 在__enter__方法中完成并在 __exit__方法.当名称表示要在__exit__方法中执行的操作时,将使用渐进式时态("-ing").
The tense used in the names of the example contexts is not arbitrary. Past tense ("-ed") is used when the name refers to an action which is done in the __enter__ method and undone in the __exit__ method. Progressive tense ("-ing") is used when the name refers to an action which is to be done in the __exit__ method.
更多推荐
上下文管理器类的命名约定(“带有"块)
发布评论