如何分享我的Python代码给他人?

0 投票
1 回答
1642 浏览
提问于 2025-04-17 17:11

我想知道有没有办法把我的Python代码分享成一个库,让别人可以用,但又不能修改它。

在iOS中,我可以生成一个静态库和所有的.h文件给别人。程序员可以通过查看.h文件来了解可以使用哪些方法,但我不知道在Python中怎么实现这个?我该怎么告诉别人可以使用哪些方法,就像.h文件那样?

1 个回答

3

在Python中,有一个约定(在PEP 8中有说明),就是给属性名前面加下划线:

def _foo():
    """You can call this function from your own code, but you
    really shouldn't because I might change or remove it at
    any time without warning"""
    modify_guts_of_module()

def __bar():
    """Hands off. This is for the module implementation, not for
    public consumption. You can't even see this unless you go out
    of your way, and why would you do that?"""
    release_the_hounds()

这是一种Python风格的做法,用来隐藏属性,并告诉别人这些属性不应该被使用。

撰写回答