如何使方法仅对同类的其他方法可访问,而对外部不可用【在Python中】?
我有一个类的定义,里面有两个方法。它的结构大概是这样的:
class Sample:
def calling-Method(self):
print "Hi"
calledMethod()
def called-Method(self):
print "How are you"
我希望 called-Mehtod(self)
这个方法不能在类外被调用。也就是说,下面的操作是不应该被允许的:
if __name__ == "__main__":
obj = Sample()
obj.called-Method() #This should not allowed.
我查了一下,发现Python并不是特别注重隐私。虽然可以用双下划线("__")来处理,但这并不是为了隐私。
有没有什么办法可以在这种情况下实现隐私呢?任何帮助都非常感谢。