Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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_Oop_Properties_Attributes - Fatal编程技术网

Python 在不同类之间动态更新类属性

Python 在不同类之间动态更新类属性,python,oop,properties,attributes,Python,Oop,Properties,Attributes,我试图管理一个目录树,它是通过Python对象的层次结构创建的。我想用JSON序列化顶级对象,这样我就可以与用户共享两件事:JSON文件和目录。我希望其他用户能够指向该目录,因此这里的问题是设置根目录,该根目录可能会在其他计算机上更改 下面是我现在拥有的一个例子: import os.path as op class Top(): def __init__(self, root_dir): self._root_dir = root_dir intop

我试图管理一个目录树,它是通过Python对象的层次结构创建的。我想用JSON序列化顶级对象,这样我就可以与用户共享两件事:JSON文件和目录。我希望其他用户能够指向该目录,因此这里的问题是设置根目录,该根目录可能会在其他计算机上更改

下面是我现在拥有的一个例子:

import os.path as op
class Top():
    def __init__(self, root_dir):
        self._root_dir = root_dir

        intop = InTop(self.base_dir)
        self.intop = intop

    @property
    def root_dir(self):
        return self._root_dir

    @root_dir.setter
    def root_dir(self, path):
        self._root_dir = path

    @property
    def base_dir(self):
        return op.join(self.root_dir, 'Top')

class InTop():
    def __init__(self, root_dir):
        self._intop_dir = op.join(root_dir, 'InTop')

    @property
    def intop_dir(self):
        return self._intop_dir

    @intop_dir.setter
    def intop_dir(self, path):
        self._intop_dir = path
我很高兴现在能够更新顶部对象中的路径:

t = Top('~/projects/')
print(t.root_dir)  # ~/projects/
print(t.base_dir)  # ~/projects/Top

t.root_dir = '~/Downloads/'
print(t.root_dir)  # ~/Downloads/
print(t.base_dir)  # ~/Downloads/Top
但是,有没有任何方法可以将该更改传播到InTop对象

t = Top('~/projects/')
print(t.root_dir)  # ~/projects/
print(t.base_dir)  # ~/projects/Top
print(t.intop.intop_dir)  # ~/projects/Top/InTop

t.root_dir = '~/Downloads/'
print(t.root_dir)  # ~/Downloads/
print(t.base_dir)  # ~/Downloads/Top
print(t.intop.intop_dir)  # ~/projects/Top/InTop   <--- How to update this?
t=Top(“~/projects/”)
打印(t.root_dir)#~/projects/
打印(t.base_dir)#~/projects/Top
打印(t.intop.intop_dir)#~/projects/Top/intop
t、 root_dir='~/Downloads/'
打印(t.root_dir)#~/下载/
打印(t.base_dir)#~/下载/Top

print(t.intop.intop_dir)#~/projects/Top/intop解决了这个问题。只需要在Top setter中设置它(也更正了我的setter)

让我明白:

t = Top('~/projects/')
print(t.root_dir)  # ~/projects/
print(t.top_dir)  # ~/projects/Top
print(t.intop.intop_dir)  # ~/projects/Top/InTop

t.root_dir = '~/Downloads/'
print(t.root_dir)  # ~/Downloads/
print(t.top_dir)  # ~/Downloads/Top
print(t.intop.intop_dir)  # ~/Downloads/Top/InTop
t = Top('~/projects/')
print(t.root_dir)  # ~/projects/
print(t.top_dir)  # ~/projects/Top
print(t.intop.intop_dir)  # ~/projects/Top/InTop

t.root_dir = '~/Downloads/'
print(t.root_dir)  # ~/Downloads/
print(t.top_dir)  # ~/Downloads/Top
print(t.intop.intop_dir)  # ~/Downloads/Top/InTop