-1

I recently switched to Python from Java for development and is still not used to some of the implicitness of Python programming.

I have a class which I have defined some class variables, how can I access the class variables within a method in Python?

class Example:
    CONSTANT_A = "A"
    
    @staticmethod
    def mymethod():
        print(CONSTANT_A)    

The above code would give me the error message: "CONSTANT_A" is not defined" by Pylance.

I know that I can make this work using self.CONSTANT_A, but self is referring to the Object, while I am trying to directly access to the Class variable (specifically constants).


Question

How can I directly access Class variables in Python and not through the instance?

Quan Bui
  • 175
  • 1
  • 13
  • 1
    IIRC, in Java, class methods are called static methods, right? If that's what's confusing you, check out [`@classmethod` vs `@staticmethod` in Python](/q/136097/4518341), and maybe [Meaning of `@classmethod` and `@staticmethod` for beginner [duplicate\]](/q/12179271/4518341) – wjandrea Nov 17 '22 at 04:39
  • Yes, that is correct. Also I don't get why in Python, I cannot access a Class variable in a method even if both the variable and method are in the same class. Why must I call `self.varname` in order to access `varname` even though I am not trying to access the Object's varname, but rather the Class's varname? @wjandrea – Quan Bui Nov 17 '22 at 04:49
  • @QuanBui this is just to tell your program which to access, class instance variable and class variable – sahasrara62 Nov 17 '22 at 04:56
  • 2
    These might help: [Can I access a class variable from an instance?](/q/25577578/4518341) and [Is accessing class variables via an instance documented?](/q/10313471/4518341) – wjandrea Nov 17 '22 at 05:03
  • why are you using staticmethod there? and why are you using a class? you should just define these things on the module level if you're not using the instance. For beginners, try to stay away from class-attributes, until you've learned what their purpose is. – Alex Dec 24 '22 at 17:45

2 Answers2

2

In python, you cannot access the parent scope (class)'s fields from methods without self. or cls..

Consider using classmethod:

class Example:
    CONSTANT_A = "A"
    
    @classmethod
    def mymethod(cls):
        print(cls.CONSTANT_A)   

or directly accessing it like Classname.attribute:

class Example:
    CONSTANT_A = "A"
    
    @staticmethod
    def mymethod():
        print(Example.CONSTANT_A)   
Jongwook Choi
  • 8,171
  • 3
  • 25
  • 22
  • 1
    Minor correction: a class is not a scope. If it were, methods could access its fields using unbound variables, like in OP's code. – wjandrea Nov 17 '22 at 04:42
  • You could also add `type(self).CONSTANT_A` for an instance method – wjandrea Nov 17 '22 at 04:44
  • Isn't `cls` just another naming for `self`? Aren't both `cls` and `self` are referring to the Object, which implicitly reference to the Class variable since the Object's variable is not available? – Quan Bui Nov 17 '22 at 04:54
  • 1
    @Quan No, not at all. It's explained in the documentation Jongwook linked. – wjandrea Nov 17 '22 at 05:07
  • 1
    In classmethods, `cls` is the "type" (it is the same object as `Example`), NOT an instance of type `Example`. You can find more details in the official python documentation. – Jongwook Choi Nov 17 '22 at 05:24
  • Thank you all, sorry about the confusion. – Quan Bui Nov 17 '22 at 05:35
1

for static method, you can access the class variable by <class_name>.<variable>.

>>> class Example:
...     CONSTANT_A = "A"
...     @staticmethod
...     def mymethod():
...         print(Example.CONSTANT_A) 
... 
>>> 
>>> x = Example.mymethod()
A # print value

wjandrea
  • 28,235
  • 9
  • 60
  • 81
sahasrara62
  • 10,069
  • 3
  • 29
  • 44