在 Python 中访问类的成员变量? [英] Accessing a class' member variables in Python?

查看:34
本文介绍了在 Python 中访问类的成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Example(object):
    def the_example(self):
        itsProblem = "problem"

theExample = Example()
print(theExample.itsProblem)

如何访问类的变量?我试过添加这个定义:

How do I access a class's variable? I've tried adding this definition:

def return_itsProblem(self):
    return itsProblem

然而,这也失败了.

推荐答案

答案,一言以蔽之

在您的示例中,itsProblem 是一个局部变量.

In your example, itsProblem is a local variable.

您必须使用 self 来设置和获取实例变量.您可以在 __init__ 方法中设置它.那么您的代码将是:

Your must use self to set and get instance variables. You can set it in the __init__ method. Then your code would be:

class Example(object):
    def __init__(self):
        self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)

但是如果你想要一个真正的类变量,那么直接使用类名:

But if you want a true class variable, then use the class name directly:

class Example(object):
    itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)
print (Example.itsProblem)

但是要小心这个,因为 theExample.itsProblem 被自动设置为等于 Example.itsProblem,但根本不是同一个变量,可以独立更改.

But be careful with this one, as theExample.itsProblem is automatically set to be equal to Example.itsProblem, but is not the same variable at all and can be changed independently.

一些解释

在 Python 中,可以动态创建变量.因此,您可以执行以下操作:

In Python, variables can be created dynamically. Therefore, you can do the following:

class Example(object):
    pass

Example.itsProblem = "problem"

e = Example()
e.itsSecondProblem = "problem"

print Example.itsProblem == e.itsSecondProblem 

打印

正确

因此,这正是您在前面的示例中所做的.

Therefore, that's exactly what you do with the previous examples.

确实,在 Python 中,我们使用 self 作为 this,但不止于此.self 是任何对象方法的第一个参数,因为第一个参数始终是对象引用.这是自动的,无论您是否称其为 self.

Indeed, in Python we use self as this, but it's a bit more than that. self is the the first argument to any object method because the first argument is always the object reference. This is automatic, whether you call it self or not.

这意味着您可以:

class Example(object):
    def __init__(self):
        self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)

或:

class Example(object):
    def __init__(my_super_self):
        my_super_self.itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)

完全一样.ANY 对象方法的第一个参数是当前对象,我们只把它称为 self 作为约定. 并且你只向这个对象添加一个变量,就像你一样从外面做.

It's exactly the same. The first argument of ANY object method is the current object, we only call it self as a convention. And you add just a variable to this object, the same way you would do it from outside.

现在,关于类变量.

当你这样做时:

class Example(object):
    itsProblem = "problem"


theExample = Example()
print(theExample.itsProblem)

您会注意到我们首先设置类变量,然后访问对象(实例)变量.我们从来没有设置这个对象变量,但它有效,这怎么可能?

You'll notice we first set a class variable, then we access an object (instance) variable. We never set this object variable but it works, how is that possible?

好吧,Python 会先尝试获取对象变量,但是如果找不到,就会给你类变量.警告:类变量在实例之间共享,而对象变量不是.

Well, Python tries to get first the object variable, but if it can't find it, will give you the class variable. Warning: the class variable is shared among instances, and the object variable is not.

作为结论,永远不要使用类变量为对象变量设置默认值.为此使用 __init__.

As a conclusion, never use class variables to set default values to object variables. Use __init__ for that.

最终,您将了解到 Python 类是实例,因此是对象本身,这为理解上述内容提供了新的见解.一旦你意识到这一点,请稍后再回来阅读.

Eventually, you will learn that Python classes are instances and therefore objects themselves, which gives new insight to understanding the above. Come back and read this again later, once you realize that.

这篇关于在 Python 中访问类的成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆