0

I am having trouble changing static variable of a class instead of just an instance.

Here is my code :

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

In another class, I would have:

GetAbsPath.absPath = "some/other/path"

Therefore, I would want to change absPath variable in a way that absPath is permanently changed so that any other class getting the absPath would get"some/other/path"

Right now, absPathwould always be the default value everytime I call GetAbsPath.getPath().

3
  • Why do your instance methods have cls argument? setPath will replace absPath only for the current instance, after which it won't "see" changes to the class attribute. Should that be a class method? Commented Jul 24, 2014 at 14:52
  • 1
    Were getPath and setPath supposed to be class methods? They're not; they're instance methods, and cls is going to be just a funny name for self. Also, why do you have class variables for this? You're making a class just to manage one string global variable. Commented Jul 24, 2014 at 14:52
  • Oh I see, yeah they are suppose to be class methods. Commented Jul 24, 2014 at 14:57

1 Answer 1

2

You need to decorate those methods with @classmethod.

Edit actually, I agree with user235712, you don't need a class here at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.