Python static method access class variable

This is what my code looks like

class InviteManager():
    ALREADY_INVITED_MESSAGE = "You are already on our invite list"
    INVITE_MESSAGE = "Thank you! we will be in touch soon"

    @staticmethod
    @missing_input_not_allowed
    def invite(email):
        try:
            db.session.add(Invite(email))
            db.session.commit()
        except IntegrityError:
            return ALREADY_INVITED_MESSAGE
        return INVITE_MESSAGE

When I run my tests, I see

NameError: global name 'INVITE_MESSAGE' is not defined

How can I access INVITE_MESSAGE inside @staticmethod?

asked Aug 25, 2013 at 16:47

Python static method access class variable

daydreamerdaydreamer

82.5k178 gold badges432 silver badges697 bronze badges

0

You can access it as InviteManager.INVITE_MESSAGE, but a cleaner solution is to change the static method to a class method:

@classmethod
@missing_input_not_allowed
def invite(cls, email):
    return cls.INVITE_MESSAGE

(Or, if your code is really as simple as it looks, you can replace the whole class with a bunch of functions and constants in a module. Modules are namespaces.)

answered Aug 25, 2013 at 16:51

3

Try:

class InviteManager():
    ALREADY_INVITED_MESSAGE = "You are already on our invite list"
    INVITE_MESSAGE = "Thank you! we will be in touch soon"

    @staticmethod
    @missing_input_not_allowed
    def invite(email):
        try:
            db.session.add(Invite(email))
            db.session.commit()
        except IntegrityError:
            return InviteManager.ALREADY_INVITED_MESSAGE
        return InviteManager.INVITE_MESSAGE

The InviteManager is in the scope of it's staticmethods.

answered Aug 25, 2013 at 16:51

Maciej GolMaciej Gol

14.9k4 gold badges32 silver badges50 bronze badges

Just realized, I needed @classmethod

class InviteManager():
    ALREADY_INVITED_MESSAGE = "You are already on our invite list"
    INVITE_MESSAGE = "Thank you! we will be in touch soon"

    @classmethod
    @missing_input_not_allowed
    def invite(cls, email):
        try:
            db.session.add(Invite(email))
            db.session.commit()
        except IntegrityError:
            return cls.ALREADY_INVITED_MESSAGE
        return cls.INVITE_MESSAGE

You can read about it here

answered Aug 25, 2013 at 16:51

Python static method access class variable

daydreamerdaydreamer

82.5k178 gold badges432 silver badges697 bronze badges

You can access to yours attributes with InviteManager.INVITE_MESSAGE and InviteManager.ALREADY_INVITED_MESSAGE without changing anything in their declaration.

answered Aug 25, 2013 at 16:51

Python static method access class variable

Maxime LorantMaxime Lorant

32.7k19 gold badges84 silver badges96 bronze badges

Simply, understand the concept of class level variables/methods and instance level variables/methods.

While working with static methods, you won't use self keyword as self keyword is used to represent instance of the class or to use instance variables of the class. In fact one use class_name, see below example:

class myclass():
    msg = "Hello World!"

    @staticmethod
    def printMsg():
        print(myclass.msg)



myclass.printMsg() #Hello World!
print(myclass.msg) #Hello World!
myclass.msg = "Hello Neeraj!"
myclass.printMsg() #Hello Neeraj!
print(myclass.msg) #Hello Neeraj!

answered Apr 7, 2019 at 6:20

Python static method access class variable

It is much simpler than all of that:

Just added __class__ to the class variables. Like so:

return __class__.ALREADY_INVITED_MESSAGE
        return __class__.INVITE_MESSAGE

There is no need to mention the ClassName (InviteManager) and there is no need to use a classmethod

answered Oct 14, 2021 at 1:50

Python static method access class variable

1

Do static methods have access to class variables Python?

A static method doesn't have access to the class and instance variables because it does not receive an implicit first argument like self and cls . Therefore it cannot modify the state of the object or class. The class method can be called using ClassName. method_name() as well as by using an object of the class.

Can a static method access class variables?

Static Methods can access class variables(static variables) without using object(instance) of the class, however non-static methods and non-static variables can only be accessed using objects. Static methods can be accessed directly in static and non-static methods.

How do you access a class variable in Python?

Use class_name dot variable_name to access a class variable from a class method in Python. Use instance to access variables outside the class.

Can instance method access class variables Python?

We can access the instance variable using the object and dot ( . ) operator. In Python, to work with an instance variable and method, we use the self keyword. We use the self keyword as the first parameter to a method.