上下文管理器类的命名约定(“带有"块)

编程入门 行业动态 更新时间:2024-10-23 19:19:07
本文介绍了上下文管理器类的命名约定(“带有"块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于要在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.

更多推荐

上下文管理器类的命名约定(“带有"块)

本文发布于:2023-11-23 10:08:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621055.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   管理器   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!