静态方法如何访问 Python 中的类变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18431313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:45:03  来源:igfitidea点击:

How can static method access class variable in Python?

python

提问by daydreamer

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_MESSAGEinside @staticmethod?

如何进入INVITE_MESSAGE内部@staticmethod

采纳答案by Fred Foo

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

您可以作为 访问它InviteManager.INVITE_MESSAGE,但更简洁的解决方案是将静态方法更改为类方法:

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

(或者,如果你的代码真的像看起来那么简单,你可以用一个模块中的一堆函数和常量替换整个类。模块是命名空间。)

回答by Maciej Gol

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 InviteManageris in the scope of it's staticmethods.

InviteManager是在它的静态方法的范围内。

回答by daydreamer

Just realized, I needed @classmethod

刚刚意识到,我需要 @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

你可以在这里阅读

回答by Maxime Lorant

You can access to yours attributes with InviteManager.INVITE_MESSAGEand InviteManager.ALREADY_INVITED_MESSAGEwithout changing anything in their declaration.

您可以在更改InviteManager.INVITE_MESSAGEInviteManager.ALREADY_INVITED_MESSAGE不更改声明中的任何内容的情况下访问您的属性。

回答by Neeraj Bansal

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

简单来说,理解类级变量/方法和实例级变量/方法的概念。

While working with static methods, you won't use selfkeyword as selfkeyword 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:

在使用静态方法时,您不会使用self关键字,因为self关键字用于表示类的实例或使用类的实例变量。实际上一个使用class_name,见下面的例子:

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!