-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?

5
  • 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, and maybe Meaning of @classmethod and @staticmethod for beginner [duplicate] Commented Nov 17, 2022 at 4: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 Commented Nov 17, 2022 at 4:49
  • @QuanBui this is just to tell your program which to access, class instance variable and class variable Commented Nov 17, 2022 at 4:56
  • 2
    These might help: Can I access a class variable from an instance? and Is accessing class variables via an instance documented? Commented Nov 17, 2022 at 5: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. Commented Dec 24, 2022 at 17:45

2 Answers 2

3

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)   
Sign up to request clarification or add additional context in comments.

6 Comments

Minor correction: a class is not a scope. If it were, methods could access its fields using unbound variables, like in OP's code.
You could also add type(self).CONSTANT_A for an instance method
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 No, not at all. It's explained in the documentation Jongwook linked.
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.
|
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.