How can static method access class variable in Python?

Python

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.)

Solution 2 - Python

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.

Solution 3 - Python

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

Solution 4 - Python

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

Solution 5 - Python

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!

Solution 6 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondaydreamerView Question on Stackoverflow
Solution 1 - PythonFred FooView Answer on Stackoverflow
Solution 2 - PythonMaciej GolView Answer on Stackoverflow
Solution 3 - PythondaydreamerView Answer on Stackoverflow
Solution 4 - PythonMaxime LorantView Answer on Stackoverflow
Solution 5 - PythonNeeraj BansalView Answer on Stackoverflow
Solution 6 - PythonMohsen BananView Answer on Stackoverflow