"私人" (实现)类 [英] "Private" (implementation) class in Python

查看:157
本文介绍了"私人" (实现)类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个由两部分组成的小型Python模块:

I am coding a small Python module composed of two parts:


  • 定义公共接口的一些功能

  • 上述函数使用的实现类,但在模块外没有意义。

起初,我决定隐藏这个实现类,使用它在函数内部定义它,但是这妨碍了可读性,如果多个函数重用同一个类,则不能使用。

At first, I decided to "hide" this implementation class by defining it inside the function using it, but this hampers readability and cannot be used if multiple functions reuse the same class.

除了评论和文字外,还有一种机制,将类标记为私人或内部?我知道下划线机制,但据我所知,它只适用于变量,函数和方法名称。

So, in addition to comments and docstrings, is there a mechanism to mark a class as "private" or "internal"? I am aware of the underscore mechanism, but as I understand it it only applies to variables, function and methods name.

推荐答案

使用一个下划线前缀:

class _Internal:
    ...

这是内部符号的官方Python约定; from module import *不会导入下划线的前缀对象。

This is the official Python convention for 'internal' symbols; "from module import *" does not import underscore-prefixed objects.

编辑:引用单个下划线约会

这篇关于"私人" (实现)类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆