dea*_*mon 2 python oop visibility
是否有关于在Python类中公开成员的一般约定?我知道这是"它取决于"的情况,但也许有一个经验法则.
私人会员:
class Node:
def __init__(self):
self.__children = []
def add_children(self, *args):
self.__children += args
node = Node()
node.add_children("one", "two")
Run Code Online (Sandbox Code Playgroud)
公共成员:
class Node2:
def __init__(self):
self.children = []
node2 = Node2()
node2.children += "one", "two"
Run Code Online (Sandbox Code Playgroud)
如果没有充分理由children私有,你会继续使用这种方法add_children吗?