是否有关于在Python类中暴露成员的常规约定?我知道这是一个“取决于”的情况,但也许有一条经验法则。暴露成员或使它们在Python中是私有的?
私有成员:
class Node:
def __init__(self):
self.__children = []
def add_children(self, *args):
self.__children += args
node = Node()
node.add_children("one", "two")
公共成员:
class Node2:
def __init__(self):
self.children = []
node2 = Node2()
node2.children += "one", "two"
如果没有很好的理由让children
私人,你会留在法add_children
?
1)它取决于2)孩子 - >孩子。 – Stephen 2010-04-29 14:38:04
uups!纠正了复数 – deamon 2010-04-29 14:40:08