【发布时间】:2019-03-22 01:24:04
【问题描述】:
我有以下代码:
def __static_func(name):
print 'Name = ' + name
class A:
def __init__(self, name):
self.name = name
def fun(self):
__static_func(self.name)
a = A('foo')
a.fun()
在 Python 2.7 上启动时,它会生成
NameError: global name '_A__static_func' is not defined
那么问题是如何从类方法中调用全局函数?
【问题讨论】:
-
在 python 3.7 中仍然发生这种情况的事实实际上很有趣。什么鬼,蟒蛇。
-
只是不要使用以双下划线开头的函数名。
-
@Bazingaa 为什么将迄今为止最好的答案作为评论发布?
-
@Aran-Fey:好的,我注意到人们不只欣赏文本作为答案。但是既然你提到了,我可以发布它
标签: python python-3.x python-2.7 class oop