Python - 更改静态变量

时间:2014-07-24 14:48:29

标签: python

我无法更改类的静态变量而不仅仅是实例。

这是我的代码:

class GetAbsPath():

    config = RawConfigParser()
    config.read('config.ini')
    absPath = config['CSVPath']['Directory']

    def __init__(self):
        pass

    def getPath(cls):
        if cls.absPath.endswith('/'):
            pass
        else:
            cls.absPath += '/'
        return cls.absPath

    def setPath(cls, pathStr):
        cls.absPath = pathStr

在另一堂课中,我会:

GetAbsPath.absPath = "some/other/path"

因此,我希望以absPath永久更改的方式更改absPath变量,以便获得absPath的任何其他类获得"some/other/path"

现在,每次拨打absPath时,GetAbsPath.getPath()始终是默认值。

1 个答案:

答案 0 :(得分:2)

您需要使用@classmethod装饰这些方法。

编辑实际上,我同意user235712,您根本不需要这里的课程。

相关问题