在Python中访问类的变量 [英] Accessing a class's variable in Python

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

问题描述

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

exampleExample =
print(theExample.itsProblem)

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

  def return_itsProblem(self):
return itsProblem



然而,这也失败了。

解决方案



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



您必须使用 self 设置和获取实例变量。您可以在 __ init __ 方法中设置它。然后你的代码是:

  class Example(object):
def __init __(self):
self.itsProblem =problem


theExample = Example()
print(theExample.itsProblem)

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

  class Example(object):
itsProblem =problem


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

但请小心这个,因为示例。 c>



一些解释



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

  class Example(object):
pass
$ b b Example.itProblem =problem

e = Example()
e.itsSecondProblem =problem

print Example.itsProblem == e.itsSecondProblem

列印


真的


因此,这正是您对前面示例所做的。



事实上,在Python中,我们使用 self 作为,但它有点多。 Self 是任何对象方法的第一个参数,因为第一个参数总是对象引用。



这表示您可以执行以下操作:

  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
$ b b
theExample = Example()
print(theExample.itsProblem)

一模一样。 ANY对象方法的第一个参数是当前对象,我们只将其调用 self 作为约定。并且只添加一个变量到这个对象



现在,关于类变量。



当你这样做:

  class Example(object):
itsProblem =problem


theExample = Example()
print(theExample.itsProblem)


$ b b

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



好吧,Python试图先获取对象变量,但如果找不到,将给你的类变量。

警告:类变量在实例之间共享,而对象变量不是。

结论是,不要使用类变量来设置默认值到对象变量。使用 __ init __



最后,你会了解到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

Yet, that fails also.

解决方案

The anwser, in a few words

In your example, itsProblem is a local variable.

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)

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.

Some explanations

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

class Example(object):
    pass

Example.itsProblem = "problem"

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

print Example.itsProblem == e.itsSecondProblem 

Prints

True

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

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.

Which means you can do:

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


theExample = Example()
print(theExample.itsProblem)

Or:

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


theExample = Example()
print(theExample.itsProblem)

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.

Now, about the class variables.

When you do:

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?

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.

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

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天全站免登陆