Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将模块私有函数划分为单独的文件_Python - Fatal编程技术网

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

Python 将模块私有函数划分为单独的文件,python,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):

我有以下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
现在
PrivateFunc1
PrivateFunc2
都是纯私有函数。但是它们应该是长函数,因此不能保存在同一个模块文件中

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

这能以任何方式做到吗


感谢您让我学习。

简单的回答是,您不能在其他文件中实现这些方法。如果您认为实现代码太长,影响到可读性和复杂性,那么您应该考虑这些功能是否需要分解成更小的组件函数。 如果您重构这些长方法并将它们分解为组件行为,您将提高可读性并降低复杂性。这反过来会使您的代码更具python风格

您不应该试图将不同的语言范例强加给python——相反,您应该学习处理某些问题/任务的python方法

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

我猜你是C或C++程序员。Python没有区分定义和实现。您不应该试图用Python编写这种类型的代码

Python是一种解释语言,因此它不需要经过编译过程,从而允许实现存在于不同的文件中

编译的程序经过以下过程:

  • 预处理
  • 汇编
  • 装配
  • 连接
解释程序逐行解释


关于“私人”班级成员的旁白: 此外,python没有严格执行类属性/方法的“private”、“protected”或“public”范围。这些范围有一些约定,但这只是一种方便,并没有严格执行

公众:
  • 任何不以前导下划线开头的属性
受保护的:
  • 以单个前导下划线开头的任何属性
私人:
  • 以两个前导下划线开头的任何属性
  • 这些属性已损坏,因此更改了原始属性名称。该属性仍然是继承的
下面是一个示例程序来演示:

#!/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

对不起……我的错!我刚刚编辑了这个问题!我可以让函数以两个前导字母开始,但我可以把它们放在一个单独的文件中,并在这个模块中使用它们吗?好吧,这是在词汇层面强制执行的:@DYZ你写这篇文章时,我正在编辑我的答案,是的,显然它们不是继承的。但是,如果您理解python中名称是如何被破坏的,您仍然可以访问它们。@charlesadis这就是为什么我说它们是在词汇级别强制执行的,而不是在语义级别强制执行的。@DYZ是的,我是在确认您所说的,而不是质疑它。
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