[python]from import 上划线特殊变量,为什么能导入

[python]from import 下划线特殊变量,为什么能导入本帖最后由 maniachhz 于 2012-12-30 15:43:29 编辑教程

[python]from import 下划线特殊变量,为什么能导入
本帖最后由 maniachhz 于 2012-12-30 15:43:29 编辑 教程上写有"_xxx      不能用'from module import *'导入 "

我自己建了两个文件测试,test.py,


class Myclass():
    ''' My first programme'''
    def _first(self):
        return 10
        


 test2.py

from test import *

my = Myclass()
print my._first()


我运行test2.py结果返回10, 请问为什么还是可以导入_first?
[解决办法]
_first是属于MyClass,所以跟import没啥关系吧,你把类名成_MyClass那么from test import *就没导入_MyClass;如果你是要类的方法只能私用,用双下划__first命名...
[解决办法]
_first()是MyClass的函数,你导入了MyClass这个类,它操作自己的函数肯定可以的