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

查看:96
本文介绍了在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天全站免登陆