Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 最具霸道的访问方式”;“私人”;来自另一个类实例的变量_Python_Private Members - Fatal编程技术网

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”变量。如果不是这样,公约正在(轻微)被滥用