将模块私有函数划分为单独的文件Python

时间:2017-01-18 05:21:21

标签: python

我有以下Python代码:

class Test:
    def __init__(self):
        pass

    def PublicFunc1(self,value):
        return self.PrivateFunc1(value)
    def PublicFunc2(self,value):
        return self.PrivateFunc2(value)

    def PrivateFunc1(self,value):
        return value * 10
    def PrivateFunc2(self,value):
        return value * 100

现在PrivateFunc1PrivateFunc2纯属私有函数。但它们应该是长函数,因此它们不能保存在同一个模块文件中。

我想从主文件中取出两个函数的代码并将其保存在单独的文件中,但也要确保它们对测试类是私有的。

这可以以任何方式完成吗?

感谢您让我学习。

1 个答案:

答案 0 :(得分:0)

简短的回答是您无法在其他文件中实现这些方法。如果您认为实现代码太长而影响可读性和复杂性,则应考虑是否需要将这些函数分解为更小的组件函数。

如果您重构这些长方法并将其分解为组件行为,则可以提高可读性并降低复杂性。这反过来会使你的代码更加pythonic。

你不应该尝试强制使用不同的语言范例到python上 - 相反,你应该学习处理某些问题/任务的pythonic方法。

请参阅python的禅(如果您不熟悉):

~$ python
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>

我猜你是C或C ++程序员。 Python没有将定义与实现区分开来。您不应该尝试在Python中编写此类代码。

Python是一种解释型语言,因此它不会通过允许实现存在于不同文件中的编译过程。

已编译的程序将执行以下过程:

  • 预处理
  • 汇编
  • 组件

解释程序逐行解释。

Aside on" Private"班级成员:

此外,python并没有严格执行"私有","受保护"或"公共"类属性/方法的范围。这些范围有一些约定,但这只是一种方便,并没有严格执行。

公开:

  • 任何不以前导下划线开头的属性。

受保护的:

  • 任何以单个前导下划线开头的属性。

私人:

  • 以两个前导下划线开头的任何属性。
  • 这些属性被破坏,因此原始属性名称已更改。该属性仍然是继承的。

这是一个示范程序:

#!/usr/bin/env python
class BaseExample(object):
    def __init__(self):
        print "In base ctor"
        self.val = 1
        print "self.val: ", self.val
        self._val = 2
        print "self._val: ", self._val
        self.__val = 3
        print "self.__val: ", self.__val
        print '-'*50

    def print_private_attr(self):
        print "Printing private attribute: "
        print '\n','+'*50, '\n'
        print self.__val


class ExampleChild(BaseExample):
    def __init__(self):
        #self = Example()
        super(ExampleChild, self).__init__()
        print "In child ctor"
        print "self.val: ", self.val
        print "self._val: ", self._val
        print "self.__val: ", self._BaseExample__val
        print '-'*50

    def change_private_inherited_attr(self, val):
        print "Printing private attribute: ",self._BaseExample__val
        print '-'*50
        self._BaseExample__val = val
        print "Printing private attribute: ", self._BaseExample__val

print 'In global scope:'
print '\n','='*50, '\n'
example = BaseExample()
print '\n','-'*50, '\n'
example2 = ExampleChild()
print '\n','='*50, '\n'
print example.__dict__
print example2.__dict__
print '\n','='*50, '\n'
print 'In global scope:'
print 'example._BaseExample__val: ', example._BaseExample__val
print 'example2._BaseExample__val: ', example2._BaseExample__val
print 'Changing value of private attr:'
example2._BaseExample__val = 10
print 'In global scope:'
print 'example2._BaseExample__val: ', example2._BaseExample__val
example2.change_private_inherited_attr(100)
print 'In global scope:'
print 'example2._BaseExample__val: ', example2._BaseExample__val

具有以下输出:

In global scope:

==================================================

In base ctor
self.val:  1
self._val:  2
self.__val:  3
--------------------------------------------------

--------------------------------------------------

In base ctor
self.val:  1
self._val:  2
self.__val:  3
--------------------------------------------------
In child ctor
self.val:  1
self._val:  2
self.__val:  3
--------------------------------------------------

==================================================

{'_val': 2, '_BaseExample__val': 3, 'val': 1}
{'_val': 2, '_BaseExample__val': 3, 'val': 1}

==================================================

In global scope:
example._BaseExample__val:  3
example2._BaseExample__val:  3
Changing value of private attr:
In global scope:
example2._BaseExample__val:  10
Printing private attribute:  10
--------------------------------------------------
Printing private attribute:  100
In global scope:
example2._BaseExample__val:  100