小编sob*_*czi的帖子

声明内部函数的大多数pythonic方法

我正在编写一个程序,其中im使用两个主要函数,但是这两个函数都使用相同的内部函数。我想知道如何以大多数pythonic方式编写它们?我的观点是将那些帮助程序隐藏在内部某处,并且不要重复帮助程序功能。

def main_function1():
    helper1()
    helper2()
    #dowork1

def main_function2()
    helper1()
    helper2()
    #dowork2

def helper1()
    #workhelp1

def helper2()
    #workhelp2

Run Code Online (Sandbox Code Playgroud)

我能找出的唯一合理的解决方案是用..私有函数声明静态类?但是由于:

Strictly speaking, private methods are accessible outside their class, 
just not easily accessible. Nothing in Python is truly private[...]
Run Code Online (Sandbox Code Playgroud)

我陷入困境,没有想法。

来自:http : //www.faqs.org/docs/diveintopython/fileinfo_private.html

主题:为什么Python的“私有”方法实际上不是私有的?

我也考虑过使用内部帮助程序和切换程序声明一个主要功能,以确定要运行哪个功能,但是我想那是很差的解决方案。

现在,我发现最准确的唯一方法是将普通类声明为:

Strictly speaking, private methods are accessible outside their class, 
just not easily accessible. Nothing in Python is truly private[...]
Run Code Online (Sandbox Code Playgroud)

输出:

#first function#
first helper
second helper
#second function#
first helper
second helper
###
first …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

6
推荐指数
1
解决办法
156
查看次数

标签 统计

python ×1

python-3.x ×1