对于Python中的私有成员和方法,我应该使用_foo(下划线)或__bar(双下划线)?
是否可以在子类中将公共方法设为私有?我不希望扩展此类的其他类能够调用某些方法.这是一个例子:
class A:
def __init__(self):
#do something here
def method(self):
#some code here
class B(A):
def __init__(self):
A.__init__(self)
#additional initialization goes here
def method(self):
#this overrides the method ( and possibly make it private here )
Run Code Online (Sandbox Code Playgroud)
从这一点开始,我不希望任何从B扩展的类能够调用method.这可能吗 ?
编辑:这是一个"逻辑"的原因是我不希望用户以错误的顺序调用方法.