Python 最具霸道的访问方式”;“私人”;来自另一个类实例的变量
假设以下类显示某种层次结构:Python 最具霸道的访问方式”;“私人”;来自另一个类实例的变量,python,private-members,Python,Private Members,假设以下类显示某种层次结构: class BaseList2D(object): def __init__(self): self._superobject = None self._subobjects = [] def InsertUnder(self, other): if self not in other._subobjects: other._subobjects.append(sel
class BaseList2D(object):
def __init__(self):
self._superobject = None
self._subobjects = []
def InsertUnder(self, other):
if self not in other._subobjects:
other._subobjects.append(self)
self._superobject = other
return True
return False
def InsertAfter(self, other):
parent = other._superobject
if not parent:
return False
parent = parent._subobjects
parent.insert(parent.index(other) + 1, self)
return True
def GetDown(self):
if not len(self._subobjects):
return
return self._subobjects[0]
def GetNext(self):
if not self._superobject:
return
stree = self._superobject._subobjects
index = stree.index(self)
if index + 1 >= len(stree):
return
return stree[index + 1]
这真的是通过访问其他对象的隐藏属性来设置其超级对象的最佳(或唯一)方法吗?属性不应由用户设置。
\u foo
只是一种命名约定。通常,会有一个属性或其他东西为您设置“private”变量。如果不是这样,公约正在(轻微)被滥用