设置python对象属性不改变值

时间:2016-08-17 13:25:28

标签: python

如果查看下面的代码,我正在创建一个FileNode并将其传递给字符串(filename)。当文件名设置器被触发时,它应该填充剩余的字段,但它似乎不起作用。

我目前只是尝试在“文件名”设置器中测试设置属性“扩展名”,但它似乎没有更改属性,为什么会这样?

import os
import pprint

class FileNode(object):
    def __init__(self, filename):
        self.filename = filename
        self.show = ""
        self.episode = ""
        self.sequence = ""
        self.shot = ""
        self.task = ""
        self.version = ""
        self.extension = ""
        self.isValid = False

    @property
    def filename(self):
        return self.filename

    @property
    def extension(self):
        return self.extension

    @filename.setter
    def filename(self, value):
        self._filename = value

        fl, ex = os.path.splitext(value)
        self._extension = "candles"

    @extension.setter
    def extension(self, value):
        self._extension = value



a = FileNode("BTMAN_1005_001_007_model_cup_v001.max")
# print (vars(a))
pp = pprint.PrettyPrinter(depth=6)
pp.pprint(vars(a))

b = FileNode("BTMAN_1005_001_007_model_v001.max")
# print (vars(b))
pp = pprint.PrettyPrinter(depth=6)
pp.pprint(vars(b))

1 个答案:

答案 0 :(得分:3)

您的媒体资源名称与属性相同!

尝试使用以单个下划线开头的名称作为属性,如

...
def __init__(self, ...):
    ...
    self._extension = ""
    ...
...
@property
def extension(self):
    return self._extension
...