1

I have encountered a use-case where I have to define all the logic/rules in a static method to be called/utilized by a class method. And within the static method, it needs to access some static variables as thresholds for comparison. In my use case, the variable is a constant that is supposed to be hard coded and should not change or overwritten.

There are two ways to do this (global variable vs class variable) and I have written up a mock example showing both ways. My question is, is there any advantage/disadvantage for either method? More generally, when you need to define a static variable, what are the things to consider before you define it as global or class variables?

# method 1
var = 'hi'
class Test:
    @staticmethod
    def staticfn():
        return var
      
    def printstatic(self):
        print(self.staticfn())

test = Test()
test.printstatic()

# method 2

class Test:
    var = 'hi'
    @staticmethod
    def staticfn():
        return Test.var
      
    def printstatic(self):
        print(self.staticfn())

test = Test()
test.printstatic()

Both methods can print string 'hi'

5
  • var isn't global, it's local to the current module. Commented Dec 1, 2020 at 5:12
  • 1
    link link Correct me if I am wrong, I think both websites define the global variable on the module level. I guess it depends on the way you define it (context-driven?). But the real question I am trying to ask for this question is would method 1 or method 2 better than the other always, and if not, what are the conditions or things to consider? Commented Dec 1, 2020 at 5:20
  • 1
    @TheLazyScripter in Python, that is what is implied by global Commented Dec 1, 2020 at 6:07
  • Implied or not it's not made global. As for the question, the only difference would be how you access or change that value if ever need be. Commented Dec 1, 2020 at 6:27
  • @TheLazyScripter I am not too familiar with these definitions as I am not coming from a computer science background. Could you give an example of how to define a global variable in python if 'var' in method 1 is not global? Commented Dec 1, 2020 at 10:22

1 Answer 1

1

Generally static variables are used when they are related only to the particular class itself.

We use these variables if they represent some common property of every instance of that class.

Defining the variable outside the class is more adequate if it is needed in other places anywhere in the module. This can be done, but also carries all the risks of shared state.

I think we cannot decide which method is good just like that.

It depends on your use case.

Note that there is also the option of passing your threshold values as arguments to the class __init__ at instantiation time.

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.