2

Is there a way to access a class variable without explicitly using the class name, in case I decided to change the name of the class later on?

Something like this

static_variable = 'stuff'

className = CLASS

def method (self):
    className.static_variable

Is this possible in a simple way?

Answer

self.static_variable or __class__.static_variable

Rethipher
  • 338
  • 1
  • 13
  • 1
    Any particular reason you're not just doing `self.TABLE`? – user2357112 supports Monica May 31 '17 at 20:27
  • Or `__class__.TABLE`, if you're going to rely on the magic `__class__` closure variable anyway? – user2357112 supports Monica May 31 '17 at 20:28
  • 2
    As a rule of thump: Inside your class you shouldn't need to know the name of the class (one exception: `super` in python-2.x and maybe in `__repr__` or `__str__`). Whenever you find you need the name of your class there's probably a better/easier way. – MSeifert May 31 '17 at 20:29
  • @user2357112 When I started this many months ago (it's part of a parsing word XML document script), I intented for variables that I might need to add would be class variables instead of instance variables. At the time I thought it made sense. In my mind, I could add more as I find them, if I need to parse more formats, or we want to change the format of our word documents. Obviously I didn't need to do that, but it's all over the place in the class now, and I don't really want to rewrite the entire thing just to change a couple of class variables to instance variables. – Rethipher May 31 '17 at 20:33
  • 1
    @Rethipher: Ignoring for the moment whether any of this stuff should be class variables or instance variables, are you not aware that you can read class variables through `self`? – user2357112 supports Monica May 31 '17 at 20:34
  • I actually wasn't aware of that. That would seem to make it more difficult though to tell whether the variable is instance or static though. That would work for what I'm trying to do, however. – Rethipher May 31 '17 at 20:38
  • 2
    @Rethipher When do you need to tell the difference? `self.foo` first checks the instance for an attribute named `foo`, and if there is none, starts looking for `foo` as a class attribute of the base class(es), in MRO order. Having instance variables and class variables of the same name would be poor design. – chepner May 31 '17 at 20:49

1 Answers1

1

For anybody looking for an answer to this, disregarding for the moment whether mixing static and instance variables is a good idea.

There are two simple ways to approach this.

First way

class MyClass():
    static_variable = 'VARIABLE'

    def __init__(self):
        self.instanceVariable = 'test'

    def access_static(self):
        print(__class__.static_variable)

Second way

class MyClass():
    static_variable = 'VARIABLE'

    def __init__(self):
        self.instanceVariable = 'test'

    def access_static(self):
        print(self.static_variable)

An instance variable can be accessed using class.static_variable, or using self.static_variable as long as an instance variable hasn't been defined for self.static_variable somewhere in the code.

Using self would make it ambiguous as to whether you are accessing a static variable or instance variable though, so my preferred way of doing it would be to simply prepend static_variable with _class_ instead of ClassName.static_variable

Rethipher
  • 338
  • 1
  • 13
  • 3
    Please stop calling them static variables. They are called class variables; there is nothing particularly static about them. – chepner May 31 '17 at 20:53
  • @chepner It really doesn't matter what you call it as long as you know what it does. I go back and forth between matlab, Simulink, python, c, c++, and sometimes java. All of them call these the concept something different. I cannot stress enough how unimportant the name is. – Rethipher May 31 '17 at 21:36
  • 1
    It's quite important. For instance, there is a big difference between static methods and class methods. *You* might know what you mean by static variable; don't assume others will. – chepner May 31 '17 at 21:39