请教python 关于函数名__与_的区别

zfzaizheli 2010-07-28 09:34:55
请问各位大虾:

在一个py脚本里函数名定义成def __test()和def _test()有什么区别呢?

好像是有私有和公有 有关系?

那import 的话还是能调用到呀,很不明白哦,请大虾们给讲解下哦~



...全文
506 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
pythonddr2 2012-01-11
  • 打赏
  • 举报
回复
这个也行啊,我晕倒,
fnzh0003 2012-01-10
  • 打赏
  • 举报
回复
学会了一招。谢谢
zengna_com 2010-12-13
  • 打赏
  • 举报
回复
单下划线的是虚函数,只允许内部使用?
I_NBFA 2010-07-31
  • 打赏
  • 举报
回复
9.6. Private Variables
“Private” instance variables that cannot be accessed except from inside an object, don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

Note that the mangling rules are designed mostly to avoid accidents; it still is possible to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger.

Notice that code passed to exec(), eval() or execfile() does not consider the classname of the invoking class to be the current class; this is similar to the effect of the global statement, the effect of which is likewise restricted to code that is byte-compiled together. The same restriction applies to getattr(), setattr() and delattr(), as well as when referencing __dict__ directly.
cjmy001 2010-07-31
  • 打赏
  • 举报
回复
我也来学习一下。多谢啊
oluckly 2010-07-29
  • 打赏
  • 举报
回复
双下划线一般用于变量,单下划线一般用于函数。类似保护成员的意思!具体的,在python核心编程上有介绍
notax 2010-07-29
  • 打赏
  • 举报
回复

http://docs.python.org/tutorial/classes.html#SECTION0011600000000000000000


不如花点时间好好看看书吧
zfzaizheli 2010-07-29
  • 打赏
  • 举报
回复
有没有再详细点的呀。或者是这方面的文档。给个地址~~
notax 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 thy38 的回复:]

顶层代码的话是没有区别的。作为类的方法时才有区别:
Python code
class Test(object):
def __test(self):
pass
def _test(self):
pass

t = Test()
t._test()
t.__test() #AttributeError
t._Test__test() #……
[/Quote]


补充一下
1.
def __test(self):
是有他的意义,python 把他自动换成 '_Test__test', 已免和其他test 的名字混淆

>>> class Test(object):
... def __test(self):
... print '__test'
...
>>> t = Test()
>>> t._Test__test()
__test
>>> print Test.__dict__
{'__dict__': <attribute '__dict__' of 'Test' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None, '_Test__test': <function __test at 0xb7d397d4>}
>>>

2.
Test._test() 习惯用此表示私有


3.
另外
from foo import *
* 不包括以 _ 开头的名字



iambic 2010-07-28
  • 打赏
  • 举报
回复
……
感觉1楼说的差不多了……
zfzaizheli 2010-07-28
  • 打赏
  • 举报
回复
恩。知道怎么解决了。

可是还是不明白为什么。能不能解释说明下啊?

def test(self):

def _test(self):

def __test(self):

分别有什么意思呢?
thy38 2010-07-28
  • 打赏
  • 举报
回复
顶层代码的话是没有区别的。作为类的方法时才有区别:
class Test(object):
def __test(self):
pass
def _test(self):
pass

t = Test()
t._test()
t.__test() #AttributeError
t._Test__test() #OK
因为Python会把__开头的函数名改成:_类名__函数名

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧